Thursday, March 4, 2010

How to add click event for dynamically generated button

Hi,

In this article iam going to discuss about adding onclick

event to a dynamically generated button.See the code below

TextBox txt = new TextBox();
Button btn = new Button();
Label lbl = new Label();
protected void Page_Load(object sender, EventArgs e)
{
lbl.Text = "Enter Text : ";
lbl.Font.Bold = true;

this.Form.Controls.Add(lbl);
this.Form.Controls.Add(txt);
btn.Text = "btnsave";
btn.Attributes.Add("runat", "server");
this.Form.Controls.Add(btn);
btn.Click += new System.EventHandler(this.Button_Click);


}

protected void Button_Click(object sender, EventArgs e)
{
Response.Write(txt.Text);

}

Thanks & Regards
Umar Daraj

No comments: