473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Nov 19 '05 #1
4 4779
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...

Nov 19 '05 #2
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
}
}

Nov 19 '05 #3
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.

Nov 19 '05 #4
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);
}

}
}
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
0
by: dina | last post by:
I am using one main aspx page, in which i am loading different user controls for different web pages. At one place, when i load a user control from another user control and come back to the same...
3
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their...
2
by: ~~~ .NET Ed ~~~ | last post by:
I have a problem (don't we all?). I have a web form with multiple modules, some of these modules have an ASP.NET (server run) button. OK, now I have UserControlX which has one such button (say...
2
by: David | last post by:
Hi all, I am dynamically creating a LinkButton in my datagrid, however, the event that I am creating for it is not firing. I am obviously missing something? What am I missing? Here is the...
3
by: Brett | last post by:
I'm using a third part component in a certain app. The component has methods and events. I noticed one particular event was not firing after a while but it should have been. The problem was the...
4
by: TS | last post by:
I am creating a User control and i create some dynamic controls in the init handler. one of the controls is a custom validator which i assign a serverValidate event handler. I usally always do my...
4
by: Dan Soendergaard | last post by:
Hello fellow developers, I've created a control which derives from the .NET TextBox. I then wanted to add two events to the new control, so I added the following to the control code: public...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.