Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Event not firing for user control inside user control (http://www.velocityreviews.com/forums/t92531-event-not-firing-for-user-control-inside-user-control.html)

vatech1993 12-10-2004 02:42 PM

Event not firing for user control inside user control
 
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==?= 12-10-2004 11:57 PM

RE: Event not firing for user control inside user control
 
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...
>
>


vatech1993 12-11-2004 04:26 AM

Re: Event not firing for user control inside user control
 
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 12-11-2004 01:27 PM

Re: Event not firing for user control inside user control
 
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.


=?Utf-8?B?U8Opcmdpbw==?= 12-11-2004 02:51 PM

Re: Event not firing for user control inside user control
 
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.ToS tring()+"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);
}

}
}




All times are GMT. The time now is 07:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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