How to assign event to dynamically generated asp:button?

H

hb

Hi,

When I add an asp:button (ex: id=btnLog) on home.aspx,
I need to create btnLog_Click() event in home.aspx.cs,
and also link this event and the button in OnInit() method
by adding:
this.btnLog.Click +=new System.EventHandler(this.btnLog_Click);

Now, I need to generate some asp:button dynamically in
an asp:table, and assign the event to all buttons. But in
the event, I need to retrieve the ID of the clicked button.

Would you please tell me:
1. how can I assign the event to a dynamically generated button?
2. how can I add the link between a specific button and the event in
OnInit()
method?
3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?

Thank you

hb
 
K

Karl Seguin

Button btn = new Button();
btn.ID = "firstButton";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

public void btnLog_Click(object sender, EventArgs e) {
string id = ((Button)sender).ID;
}


The controls need to be created on postback as well, so no wrapping it in a
if (!Page.IsPostBack) {}

Karl
 
A

Amar

1. how can I assign the event to a dynamically generated button?

OnInit Event of form add

Button btn1 = new Button();
btn1.ID = "Test";
btn1.Click += new EventHandler(MyButtonClick);
LocateForm(this,btn1); -- FUNCTION TO ADD THE BUTTON

private void LocateForm(Control ctrl, Button btn){
foreach(Control mCtrl in ctrl.Controls){
if (mCtrl.GetType() == typeof(HtmlForm)){
mCtrl.Controls.Add(btn);
}else{
LocateForm(mCtrl,btn);
}
}
}

private void MyButtonClick(object sender, System.EventArgs e){
// DO SOMETHING
}

3. how can I retrieve the ID of the clicked button(i.e. the button
that fires the event)?

On The Previous function
private void MyButtonClick(object sender, System.EventArgs e){
string buttonID = ((Button)sender).ID;
// DO SOMETHING
}
 
H

hb

Hi, Karl,

It works! Thank you for the help!

HB
Karl Seguin said:
Button btn = new Button();
btn.ID = "firstButton";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

btn = new Button();
btn.ID = "second";
btn.Click += new System.EventHandler(this.btnLog_Click);
SomeParentControl.Controls.Add(btn);

public void btnLog_Click(object sender, EventArgs e) {
string id = ((Button)sender).ID;
}


The controls need to be created on postback as well, so no wrapping it in a
if (!Page.IsPostBack) {}

Karl
 
H

hb

Hi, Amar,

It works! Thank you for your help!

hb
Amar said:
OnInit Event of form add

Button btn1 = new Button();
btn1.ID = "Test";
btn1.Click += new EventHandler(MyButtonClick);
LocateForm(this,btn1); -- FUNCTION TO ADD THE BUTTON

private void LocateForm(Control ctrl, Button btn){
foreach(Control mCtrl in ctrl.Controls){
if (mCtrl.GetType() == typeof(HtmlForm)){
mCtrl.Controls.Add(btn);
}else{
LocateForm(mCtrl,btn);
}
}
}

private void MyButtonClick(object sender, System.EventArgs e){
// DO SOMETHING
}



On The Previous function
private void MyButtonClick(object sender, System.EventArgs e){
string buttonID = ((Button)sender).ID;
// DO SOMETHING
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top