Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Event of a dynamic generated button

Author  Topic 

onlyforme
Starting Member

25 Posts

Posted - 2008-12-17 : 06:17:13
Hi friends,
In my application i want to generate dynamic textboxes and a button.And in that dynamically generated button click i need to generate another set of dynamic textbox and button.How to get the event of the dynamic button.I tried several ways,like adding click event dynamically when it is generated.But the problem is that when i click on the dynamic button the page get refreshed.The dynamic generated textbox and button disappeared.My code is as follows

protected void Button1_Click(object sender, EventArgs e)
{
int cnt = int.Parse(TextBox1.Text.ToString());

TableRow tr = new TableRow();
TableCell td = new TableCell();
for (int j = 1; j <= cnt; j++)
{

TextBox txt = new TextBox();
txt.ID = "txt" + j.ToString();
td.Controls.Add(txt);
}
Button btn = new Button();
btn.ID = "btn1";
btn.Text = "ADD";
btn.CommandArgument = "1";
btn.Click += new EventHandler(MyButtonClick);
td.Controls.Add(btn);
tr.Cells.Add(td);
tr.VerticalAlign = VerticalAlign.Top;

tbldyanamic.Rows.Add(tr);


}
private void MyButtonClick(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string buttonID = ((Button)sender).ID;
}
// DO SOMETHING
}
Plz help me to solve this problem.

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-12-18 : 04:06:14
Add the control in page_preload & also add the handler in the page_preload.
Go to Top of Page

onlyforme
Starting Member

25 Posts

Posted - 2008-12-18 : 05:07:05
Hi
I tried like tht,but its not working.Can u plz give me a sample code
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-12-18 : 05:41:07
protected void Page_PreLoad(object sender, System.EventArgs e)
{
Button btn;
btn = new Button();
btn.ID = "btn1";
btn.Text = "ADD";
btn.CommandArgument = "1";
btn.Click += (MyButtonClick);
td.Controls.Add(btn);
tr.Cells.Add(td);
tr.VerticalAlign = VerticalAlign.Top;
}

private void MyButtonClick(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string buttonID = ((Button)sender).ID;
}
// DO SOMETHING
}

Go to Top of Page

onlyforme
Starting Member

25 Posts

Posted - 2008-12-19 : 04:08:43
Thanku very much .It helps me a lot.
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-12-19 : 06:06:49
quote:
Originally posted by onlyforme

Thanku very much .It helps me a lot.


Is it working now?
Go to Top of Page

onlyforme
Starting Member

25 Posts

Posted - 2008-12-22 : 07:11:17
hi,
ur code helps me a lot.Its work for me ayama
Go to Top of Page

onlyforme
Starting Member

25 Posts

Posted - 2008-12-22 : 07:12:31
quote:
Originally posted by ayamas

quote:
Originally posted by onlyforme

Thanku very much .It helps me a lot.


Is it working now?


s ayamas its works
Go to Top of Page
   

- Advertisement -