Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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: 3.4/5 (8 votes cast)