Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to assign event to dynamically generated asp:button?

Reply
Thread Tools

How to assign event to dynamically generated asp:button?

 
 
hb
Guest
Posts: n/a
 
      11-04-2004
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


 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      11-05-2004
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
--
MY ASP.Net tutorials
http://www.openmymind.net/


"hb" <> wrote in message
news:...
> 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
>
>



 
Reply With Quote
 
 
 
 
Amar
Guest
Posts: n/a
 
      11-05-2004
> 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
}
 
Reply With Quote
 
hb
Guest
Posts: n/a
 
      11-05-2004
Hi, Karl,

It works! Thank you for the help!

HB
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:...
> 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
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "hb" <> wrote in message
> news:...
> > 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
> >
> >

>
>



 
Reply With Quote
 
hb
Guest
Posts: n/a
 
      11-05-2004
Hi, Amar,

It works! Thank you for your help!

hb
"Amar" <> wrote in message
news: om...
> > 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
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiring up event to a dynamically generated control Sosh ASP .Net 5 11-16-2005 01:10 PM
How to dynamically assign event handler functs w/ parameters? N. Demos Javascript 3 02-28-2005 08:49 PM
Dynamically assign event handler functs w/ parameters N. Demos Javascript 0 02-24-2005 03:05 PM
Event handling for dynamically generated controls Steve Caliendo ASP .Net 2 03-01-2004 06:05 PM
dynamically assign event to element Eric Javascript 4 12-27-2003 09:59 PM



Advertisments