Label Click
-
I am new to asp and I want a clickable label within another control public class MyLabel : Label, IPostBackEventHandler{ public event EventHandler Click; protected virtual void onclick(EventArgs e){ if(this.Click != null){ this.Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ onclick(new EventArgs()); } protected override void AddAttributesToRender(HtmlTextWriter writer) base.AddAttributesToRender (writer); writer.AddAttribute(HtmlTextWriterAttribute.onclick, Page.GetPostBackEventReference(this)); } } In my other control I have the following: protected override void Render(HtmlTextWriter output) { MyLabel ML=new MyLabel(); ML.Text = "TEST"; ML.Click += new System.EventHandler(this.ML_Click); base.Controls.Add(ML); base.Render(output); } private void ML_Click(object sender, EventArgs e) { Console.WriteLine(e.ToString()); } When I click on MyLabel in the main control it fires but it never gets into ML_Click and AddAttributesToRender but not anywhere else, what am I missing? any ideas?
-
I am new to asp and I want a clickable label within another control public class MyLabel : Label, IPostBackEventHandler{ public event EventHandler Click; protected virtual void onclick(EventArgs e){ if(this.Click != null){ this.Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ onclick(new EventArgs()); } protected override void AddAttributesToRender(HtmlTextWriter writer) base.AddAttributesToRender (writer); writer.AddAttribute(HtmlTextWriterAttribute.onclick, Page.GetPostBackEventReference(this)); } } In my other control I have the following: protected override void Render(HtmlTextWriter output) { MyLabel ML=new MyLabel(); ML.Text = "TEST"; ML.Click += new System.EventHandler(this.ML_Click); base.Controls.Add(ML); base.Render(output); } private void ML_Click(object sender, EventArgs e) { Console.WriteLine(e.ToString()); } When I click on MyLabel in the main control it fires but it never gets into ML_Click and AddAttributesToRender but not anywhere else, what am I missing? any ideas?
Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the
"Render"
phase occurs after the"Handle postback events"
, so when theClick
event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding theRender
method, you should override theCreateChildControls
method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:protected override void CreateChildControls()
{
MyLabel ML = new MyLabel();
ML.Text = "TEST";
ML.Click += new System.EventHandler(this.ML_Click);Controls.Add(ML);
}
For more information, you can see Developing ASP.NET Server Controls[^]
-
Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the
"Render"
phase occurs after the"Handle postback events"
, so when theClick
event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding theRender
method, you should override theCreateChildControls
method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:protected override void CreateChildControls()
{
MyLabel ML = new MyLabel();
ML.Text = "TEST";
ML.Click += new System.EventHandler(this.ML_Click);Controls.Add(ML);
}
For more information, you can see Developing ASP.NET Server Controls[^]
-
Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the
"Render"
phase occurs after the"Handle postback events"
, so when theClick
event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding theRender
method, you should override theCreateChildControls
method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:protected override void CreateChildControls()
{
MyLabel ML = new MyLabel();
ML.Text = "TEST";
ML.Click += new System.EventHandler(this.ML_Click);Controls.Add(ML);
}
For more information, you can see Developing ASP.NET Server Controls[^]
-
May be the answer is called INamingContainer...oof. But it still fires the main control from the begining. I would think that it would just call ML_Click.
Yes, you need to make sure that your parent custom control implement the
INamingContainer
interface so that it can route the postback event to the its child controlMyLabel
. Also, you should remember that you'd nomally override theRender
method when you want to control the interface of the custom control, that's not where you create a child control which needs to process a post back event. For more information, you can see Developing a Composite Control[^]