Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Add dynamic controls with events

From Wiki

Jump to: navigation, search

Summary: Add dynamic controls and associate an event with an existing function

Need help with ASP.NET? Come and ask a question in our ASP.NET Forum

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.     Dim btn As Button
  3.  
  4.     For i As Integer = 0 To 10
  5.         btn = New Button
  6.         btn.Text = "Button" & i
  7.         btn.ID = "Button" & i
  8.         AddHandler btn.Click, AddressOf btn_Click
  9.         MyPanel.Controls.Add(btn)
  10.     Next
  11.  
  12. End Sub
  13.  
  14. Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  15.     Response.Write("You clicked " & CType(sender, Button).ID)
  16. End Sub


This Hack is part of the ASP.NET Hacks collection

437 Rating: 5.0/5 (1 vote cast)