Event not firing for user control inside user control

Discussion in 'ASP .Net' started by vatech1993, Dec 10, 2004.

  1. vatech1993

    vatech1993 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...
    vatech1993, Dec 10, 2004
    #1
    1. Advertising

  2. Did you implement the Interface IPostBackEventHandler in the class?
    If not you need to implement.

    Sérgio

    "vatech1993" 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...
    >
    >
    =?Utf-8?B?U8Opcmdpbw==?=, Dec 10, 2004
    #2
    1. Advertising

  3. vatech1993

    vatech1993 Guest

    I hadn't but unfortunately it didn't seem to change anything. The full
    code is listed below:
    namespace TestApp
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.UI;

    /// <summary>
    /// Summary description for ChildControl.
    /// </summary>
    public class ChildControl : System.Web.UI.UserControl,
    IPostBackEventHandler
    {

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    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(); //this is the line
    that causes the problem
    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();
    }

    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
    // TODO: Add ChildControl.OnBubbleEvent implementation
    return base.OnBubbleEvent (source, args);
    }
    #region IPostBackEventHandler Members

    public void RaisePostBackEvent(string eventArgument)
    {
    CommandEventArgs e = new CommandEventArgs("Click", eventArgument);
    OnClick(this, e);
    }

    #endregion
    }
    }
    vatech1993, Dec 11, 2004
    #3
  4. vatech1993

    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
    #4
  5. I use like you can see in the code below. You don't have to implement
    IPostBackEventHandler because you define the method OnClick in the control
    but if you want to define on the page you have to do like this.



    public class DelayButton : System.Web.UI.WebControls.WebControl ,
    INamingContainer , IPostBackEventHandler
    {
    private string _name;
    private HtmlButton _visivel;
    private Button _hidden;
    private LiteralControl _space;
    /// <summary>
    ///
    /// </summary>
    public event EventHandler Click;

    /// <summary>
    ///
    /// </summary>
    [Bindable(true), Category("Data"), DefaultValue("")]
    public string ButtonLabel
    {
    get
    {
    return _name;
    }

    set
    {
    _name = value;
    }
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="e"></param>
    protected override void OnDataBinding(EventArgs e)
    {
    base.OnDataBinding (e);
    }


    /// <summary>
    ///
    /// </summary>
    protected override void CreateChildControls()
    {
    this._hidden=new Button();
    if (Click!=null)
    this._hidden.Click+=new EventHandler(this.Click);
    this._hidden.Height=Unit.Pixel(1);
    this._hidden.Width=Unit.Pixel(1);
    this._hidden.ID="Hidden";
    this.Controls.Add(_hidden);
    this._space=new LiteralControl(" ");
    this.Controls.Add(_space);
    this._visivel=new HtmlButton();
    if (this.ButtonLabel=="")
    this.ButtonLabel="Button";
    this._visivel.InnerText=this._name;
    this._visivel.ID="Visivel";
    this._visivel.Attributes.Add("onClick",this.ID.ToString()+"Click()");
    this.Controls.Add(_visivel);
    }


    /// <summary>
    ///
    /// </summary>
    private void AdicionaControls()
    {
    if (this._hidden==null)
    {
    this._hidden=new Button();
    this._hidden.Text="button_hidden";
    this.Controls.Add(_hidden);
    }

    if (this._space==null)
    {
    this._space=new LiteralControl(" ");
    this.Controls.Add(_space);
    }

    if (this._visivel==null)
    {
    this._visivel=new HtmlButton();
    this._visivel.InnerText="button_visivel";
    this.Controls.Add(_visivel);
    }

    if (this.ButtonLabel=="")
    this.ButtonLabel="Button";
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender (e);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="output"></param>
    protected override void Render(HtmlTextWriter output)
    {
    AdicionaControls();

    base.Render(output);
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="e"></param>
    public virtual void OnClick(EventArgs e)
    {
    if (Click!=null)
    {
    Click(this,e);
    }
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="eventArgument"></param>
    public void RaisePostBackEvent(string eventArgument)
    {
    OnClick(EventArgs.Empty);
    }

    }
    }
    =?Utf-8?B?U8Opcmdpbw==?=, Dec 11, 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. Vikram Sinha
    Replies:
    0
    Views:
    461
    Vikram Sinha
    Apr 12, 2004
  2. Vikram Sinha
    Replies:
    0
    Views:
    502
    Vikram Sinha
    Apr 12, 2004
  3. Don
    Replies:
    3
    Views:
    5,450
    Karl Seguin
    Mar 11, 2005
  4. murl
    Replies:
    6
    Views:
    4,479
  5. Event not firing in usercontrol inside usercontrol

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

Share This Page