Event not firing in usercontrol inside usercontrol

Discussion in 'ASP .Net Building Controls' started by vatech1993@yahoo.com, Dec 10, 2004.

  1. Guest

    I'm stumped on this problem. I've created a user control that
    dynamically creates 5 linkbuttons in the CreateChildControls method.
    Each of these child controls is linked to a commandeventhandler, has
    command name and argument attached and is assigned a unique id. If I
    use this control on a web form everything works fine, the event fires
    as planned. However if I contain the control inside another user
    control, the event on the linkbutton does not fire. The problem seems
    to be tied to the line where the ID property is assigned. If this line
    is commented out, the event fires off. Unfortunately I need each of
    the linkbuttons to have a unique id so I can access them at the time of
    the event. The code is as follows:

    protected override void CreateChildControls()
    {
    for (int x=0;x<5;x++)
    {
    LinkButton lb = new LinkButton();
    lb.Text = "Button " + x.ToString();
    lb.ID = this.UniqueID + "lb" + x.ToString();
    lb.Command += new CommandEventHandler(OnClick);
    lb.CommandName = "Click";
    lb.CommandArgument = x.ToString();
    this.Controls.Add(lb);
    lb.Dispose();
    }
    base.CreateChildControls ();
    }
    protected void OnClick(object sender, CommandEventArgs e)
    {
    LinkButton linkClicked = (LinkButton) this.FindControl(this.UniqueID
    + "lb" + e.CommandArgument.ToString());
    this.Response.Write("You clicked button " +
    e.CommandArgument.ToString());
    //do something to control here
    linkClicked.Dispose();
    }

    Any help would be greatly appreciated, I've been beating my head
    against the wall for too long on this one...
     
    , Dec 10, 2004
    #1
    1. Advertising

  2. vatech1993 Guest

    I figured it out. When I assign an ID to the linkbuttons, if I remove
    the this.UniqueID from the name everything works fine. I was adding
    this to make sure I could have multiple controls of the same type on a
    form. What I didn't realize was that the framework was doing the same
    thing for me. I didn't even need to derive the class from
    IPostBackEventHandler. I'm not sure when is the correct time to use it
    so any suggestions on when is appropriate would be appreciated.
     
    vatech1993, Dec 11, 2004
    #2
    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. nail
    Replies:
    0
    Views:
    1,569
  2. nitin khungar

    Event not firing.adding dynamic controls to Usercontrol.

    nitin khungar, Jul 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    218
    nitin
    Jul 21, 2003
  3. nitin

    Event not firing. Adding controls dynamically to UserControl

    nitin, Jul 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    188
    Patrick Sullivan
    Jul 23, 2003
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    200
  5. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    171
Loading...

Share This Page