Dynamically create and button and bind onclick event

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

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




protected void Page_Load(object sender, EventArgs e)
{
Button MyButton = new Button();
MyButton.ID="btn";
MyButton.Text = "hhhhh";
MyButton.Click += new EventHandler(MyButton_Click);
p1.Controls.Add(MyButton);
}

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





Thanks and Regards
Akiii

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
Akiii

Login to post response