Event not firing in usercontrol inside usercontrol

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Event not firing in usercontrol inside usercontrol

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

    vatech1993@yahoo.com Guest

  2. Similar Questions and Discussions

    1. Can we use a usercontrol inside a usercontrol
      hi group can we use a usercontrol inside a usercontrol. i mean can we use <%Register tagprefix..... src=....ascx%> in an ascx file. thanks in...
    2. LoadViewState override not firing in UserControl
      We're trying to get an override void LoadViewState to fire in a user control that has been added dynamically to the page. The SaveViewState seems...
    3. Event not firing. Adding controls dynamically to UserControl
      I am adding controls to the UserControl dynamically and then loading the UserControl Dynamically.But I am facing problem with firing of click event...
    4. Event not firing.adding dynamic controls to Usercontrol.
      I am adding controls to the UserControl dynamically and then loading the UserControl Dynamically.But I am facing problem with firing of click event...
    5. Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack
      a uscontrol test.ascx have a dropdownlist web control the dropdownlist's AutoPostBack property is set "true" but when i use...
  3. #2

    Default Event not firing in usercontrol inside usercontrol

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

    vatech1993@yahoo.com Guest

  4. #3

    Default Event not firing in usercontrol inside usercontrol

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

    vatech1993@yahoo.com Guest

  5. #4

    Default Re: Event not firing in usercontrol inside usercontrol

    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 Guest

  6. #5

    Default Re: Event not firing in usercontrol inside usercontrol


    Hello

    I have the same problem, sorry that I can't help you

    can you let me know if you solved it?

    BTW, i am currently investigating if it may be that dynamic events of a
    control need to be declared (or mapped) in the form.

    thanks


    [email]vatech1993@yahoo.com[/email] wrote:
    > *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... *


    --
    mglorian
    ------------------------------------------------------------------------
    Posted via [url]http://www.codecomments.com[/url]
    ------------------------------------------------------------------------

    mglorian Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139