Small handler Problem in VB.net

Joined
Jan 12, 2012
Messages
2
Reaction score
0
i have a simple asp page which has a button whose onclick event creates another button dynamically and adds the handler to it.

Everything runs fine except that the handler does not get attached. Any help guyzz.. stuck badly on this.

WebForm1.aspx

HTML:
@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="testStudent.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="text1" runat="server" Height="24px" Width="338px" text="Start by clicking the button"></asp:TextBox>
<asp:Button id="b1" onClick="MakeButton" runat="server" text="Make" />
</div>
<p>
<asp:PlaceHolder ID="pl" runat="server"></asp:PlaceHolder>
</p>
</form>
</body>
</html>

WebForm1.aspx.vb

Code:
Public Partial Class WebForm1
   Inherits System.Web.UI.Page
 
Public Sub MakeButton(ByVal sender As Object, ByVal e As EventArgs)
   Dim b2 As Button = New Button()
   b2.Text = "New Button"
   AddHandler b2.Click, AddressOf Onb2Click
   pl.Controls.Add(b2)
   text1.Text = "Now click the second button..."
End Sub
 
Private Sub Onb2Click(ByVal sender As Object, ByVal e As EventArgs)
   text1.Text = "New Button (b2) Was Clicked!!"
End Sub
 
End Class
 
Back
Top