Event not firing for user control inside user control

V

vatech1993

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

Guest

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

Sérgio
 
V

vatech1993

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

vatech1993

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

Guest

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);
}

}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top