How to assign event to dynamically generated asp:button?

Discussion in 'ASP .Net' started by hb, Nov 4, 2004.

  1. hb

    hb Guest

    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
    hb, Nov 4, 2004
    #1
    1. Advertising

  2. hb

    Karl Seguin Guest

    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
    >
    >
    Karl Seguin, Nov 5, 2004
    #2
    1. Advertising

  3. hb

    Amar Guest

    > 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
    }
    Amar, Nov 5, 2004
    #3
  4. hb

    hb Guest

    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
    > >
    > >

    >
    >
    hb, Nov 5, 2004
    #4
  5. hb

    hb Guest

    Hi, Amar,

    It works! Thank you for your help!

    hb
    "Amar" <> wrote in message
    news:...
    > > 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
    > }
    hb, Nov 5, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Caliendo
    Replies:
    2
    Views:
    453
    Mohamed El Ashmawy
    Mar 1, 2004
  2. Matthew MacDonald

    oncommand of dynamically generated button

    Matthew MacDonald, Sep 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    459
    Matthew MacDonald
    Sep 7, 2004
  3. Eric
    Replies:
    4
    Views:
    157
    Thomas 'PointedEars' Lahn
    Dec 27, 2003
  4. N. Demos
    Replies:
    0
    Views:
    108
    N. Demos
    Feb 24, 2005
  5. N. Demos
    Replies:
    3
    Views:
    174
    N. Demos
    Feb 28, 2005
Loading...

Share This Page