Event handlers and dynamically created controls

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZSBSYW5k?=, Jan 25, 2007.

  1. I have created a method that dynamically creates a single button control (as
    a proof-of-concept, I would like to be able to create multiple buttons). I
    set properties on this control including an event handler for the click
    event. The problem is that when I click the button on the page and view the
    locals window in debug mode, I don't see any information on the button
    control.

    Say, I want to create a variable number of buttons based on some user input,
    these buttons would be created with an ID=Btn1, ID=Btn2, etc. And say that I
    want to have all of these buttons call the same event handler when clicked.
    In this event handler I would like to be able to interrogate the button
    object that raised the event to determine which id number it is.
    Here is the code:

    private void createControl()
    {
    // Create and configure a single button control.
    Button btn = new Button();
    btn.ID = "btn1";
    btn.Width = Unit.Pixel(75);
    btn.Height = Unit.Pixel(25);
    btn.Text = "Click Me";
    EventArgs e = new EventArgs();
    btn.Click += new EventHandler(this.btn_Click);
    btn.Style.Add(HtmlTextWriterStyle.Left,"100px");
    btn.Style.Add(HtmlTextWriterStyle.Top,"100px");
    btn.Style.Add(HtmlTextWriterStyle.Position,"absolute");

    // Add this control to the form.
    this.form1.Controls.Add(btn);
    }

    private void btn_Click(object sender, System.EventArgs e)
    {
    Response.Write("Hello from dynamic control: ");
    }

    Any ideas, or comments would be greatly appreciated!
    Thanks,
    - Mike
     
    =?Utf-8?B?TWlrZSBSYW5k?=, Jan 25, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?TWlrZSBSYW5k?=

    senfo Guest

    Mike Rand wrote:

    > In this event handler I would like to be able to interrogate the button
    > object that raised the event to determine which id number it is.
    > Here is the code:
    > private void btn_Click(object sender, System.EventArgs e)
    > {
    > Response.Write("Hello from dynamic control: ");
    > }


    private void btn_Click(object sender, System.EventArgs e)
    {
    Button btn;

    if ((btn = sender as Button) != null)
    {
    Response.Write("Hell from dynamic control: " + btn.ID);
    }
    }

    Hope that helps,

    --
    Sean

    website: http://senfo.blogspot.com
     
    senfo, Jan 26, 2007
    #2
    1. Advertising

  3. Sean,
    Thanks, that worked perfectly!
    - Mike

    "senfo" wrote:

    > Mike Rand wrote:
    >
    > > In this event handler I would like to be able to interrogate the button
    > > object that raised the event to determine which id number it is.
    > > Here is the code:
    > > private void btn_Click(object sender, System.EventArgs e)
    > > {
    > > Response.Write("Hello from dynamic control: ");
    > > }

    >
    > private void btn_Click(object sender, System.EventArgs e)
    > {
    > Button btn;
    >
    > if ((btn = sender as Button) != null)
    > {
    > Response.Write("Hell from dynamic control: " + btn.ID);
    > }
    > }
    >
    > Hope that helps,
    >
    > --
    > Sean
    >
    > website: http://senfo.blogspot.com
    >
     
    =?Utf-8?B?TWlrZSBSYW5k?=, Jan 26, 2007
    #3
    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. =?Utf-8?B?U2hpanUgUG95aWxpbA==?=

    Problem with Event handlers for Dynamic created Controls in DatGri

    =?Utf-8?B?U2hpanUgUG95aWxpbA==?=, May 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    608
  2. Dica
    Replies:
    1
    Views:
    415
    bruce barker
    Jun 20, 2007
  3. msimmons
    Replies:
    0
    Views:
    528
    msimmons
    Jul 16, 2009
  4. Dave R

    event handlers for dynamically-added controls?

    Dave R, Mar 17, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    148
    Jason Bentley
    Mar 17, 2005
  5. Adi
    Replies:
    2
    Views:
    199
Loading...

Share This Page