Dynamically create and button and bind onclick event

Posted by Akiii under ASP.NET category on | Points: 40 | Views : 1478
<asp:Panel ID="p1" runat="server">

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
Button MyButton = new Button();
MyButton.Text = "hhhhh";
MyButton.Click += new EventHandler(MyButton_Click);

protected void MyButton_Click(object sender, EventArgs e)
Label1.Text = "Hi";

Thanks and Regards

Comments or Responses

Posted by: Vasanthmvp on: 8/15/2012 Level:Starter | Status: [Member] | Points: 10
Hi Akiii,

Could i know, where this type of scenario may i come across to dynamically create a button and bind a onclick event in real time.

Posted by: Akiii on: 8/15/2012 Level:Bronze | Status: [Member] | Points: 10
It depends upon the situation. But i can say some frameworks in Microsoft it is used. It is a good thing to create everything dynamic....

Thanks and regards

Login to post response