I. Single Button
HTML code in .aspx file
<asp:Label ID="lblHi" runat="server" Text="Hi"></asp:Label><div id="div1" runat = "server" width="50%"></div>
VB.Net code in code behind file
Private btnDynamic As ButtonProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadTrybtnDynamic = New ButtonbtnDynamic.ID = "btnDynamic”btnDynamic.Text = "I am dynamic”AddHandler btnDynamic.Click, New EventHandler(AddressOf btnDynamic_Click)div1.Controls.Add(btnDynamic)Catch ex As ExceptionEnd TryEnd SubPrivate Sub btnDynamic _Click(ByVal sender As Object, ByVal e As EventArgs)lblHi.Text = "You clicked me. :(”
End Sub
II. Multiple Buttons (loop)
HTML code in .aspx file
<asp:Label ID="lblHi" runat="server" Text="Hi"></asp:Label><div id="div1" runat = "server" width="50%"></div>
VB.Net code in code behind file
Private btnDynamic As ButtonProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadTryGetButtons()Catch ex As ExceptionEnd TryEnd SubProtected Sub GetButtons()TryDim objQry As New MyClassDim ds As New DataSetDim dt As New DataTableDim dr As DataRowds = objQry.GetButtonInformation()If ds.Tables(0).Rows.Count > 0 Thendt = ds.Tables(0)For Each dr In dt.RowsbtnDynamic = New ButtonbtnDynamic.ID = "btn" & dr("Button_No")btnDynamic.Text = dr("Button_Name")AddHandler btnDynamic.Click, New EventHandler(AddressOf btnDynamic_Click)div1.Controls.Add(btnDynamic)NextEnd Ifds.Dispose()Catch ex As ExceptionEnd TryEnd Sub
Private Sub btnDynamic _Click(ByVal sender As Object, ByVal e As EventArgs)Dim btn As Button = CType(sender, Button)lblHi.Text = "You clicked button : " & btn.ID
End Sub
No comments:
Post a Comment