473,408 Members | 2,405 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,408 software developers and data experts.

Custom control's click event

Hi, I have built a custom control button which inherits from WebControl and
implements IPostBackEventHandler. The control also declares an event Click
and provides a method OnClick which invokes the registered delegate. All is
fine with my control until I try to add it to the page dynamically in a code
behind event handler and wire-up the click event using the normal syntax such
that;

protected void btnNew_Click(object sender, EventArgs e)
{

...

BDTextButton bdtbSave = new BDTextButton();
...
...
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

When I do this the PostBack occurs but the event isn’t handled by the
handler I provid, i.e. the event didn’t seem to register.
After reading many posts I thought this may be because I was trying to
register the event too late in the page’s lifecycle. But, as a test I
dynamically added a Button webcontrol alongside my customcontrol and wired-up
an event handler for this which worked!

For further info my control is not a composite control it uses the Render
event to write it’s content using the HtmlTextWriter.

Why doesn’t my custom control’s event get registered when a webcontrol does?
Mar 23 '07 #1
4 2510
I'm afraid you'll probably need to post the code for the control itself,
rather than an example of how you are using it.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Mark" wrote:
Hi, I have built a custom control button which inherits from WebControl and
implements IPostBackEventHandler. The control also declares an event Click
and provides a method OnClick which invokes the registered delegate. All is
fine with my control until I try to add it to the page dynamically in a code
behind event handler and wire-up the click event using the normal syntax such
that;

protected void btnNew_Click(object sender, EventArgs e)
{

..

BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

When I do this the PostBack occurs but the event isn’t handled by the
handler I provid, i.e. the event didn’t seem to register.
After reading many posts I thought this may be because I was trying to
register the event too late in the page’s lifecycle. But, as a test I
dynamically added a Button webcontrol alongside my customcontrol and wired-up
an event handler for this which worked!

For further info my control is not a composite control it uses the Render
event to write it’s content using the HtmlTextWriter.

Why doesn’t my custom control’s event get registered when a webcontrol does?

Mar 23 '07 #2
Howdy,

Dynamically created controls are not persited bewteen postback. You have to
recreate them go make postback event handler fire:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && RecreateDynamicButton)
{
CreateDynamicButton();
}
}

private void CreateDynamicButton()
{
BDTextButton bdtbSave = new BDTextButton();
...
...
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

placeHodler.Controls.Add(bdtbSave);

}

protected void btnNew_Click(object sender, EventArgs e)
{
...
CreateDynamicButton();

}

private bool RecreateDynamicButton
{
get
{
object value = ViewState["RecreateDynamicButton"];
return value == null ? false : (bool) value;
}
set
{
ViewState["RecreateDynamicButton"] = value;
}
}

Hope this helps
--
Milosz
"Mark" wrote:
Hi, I have built a custom control button which inherits from WebControl and
implements IPostBackEventHandler. The control also declares an event Click
and provides a method OnClick which invokes the registered delegate. All is
fine with my control until I try to add it to the page dynamically in a code
behind event handler and wire-up the click event using the normal syntax such
that;

protected void btnNew_Click(object sender, EventArgs e)
{

..

BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

When I do this the PostBack occurs but the event isn’t handled by the
handler I provid, i.e. the event didn’t seem to register.
After reading many posts I thought this may be because I was trying to
register the event too late in the page’s lifecycle. But, as a test I
dynamically added a Button webcontrol alongside my customcontrol and wired-up
an event handler for this which worked!

For further info my control is not a composite control it uses the Render
event to write it’s content using the HtmlTextWriter.

Why doesn’t my custom control’s event get registered when a webcontrol does?

Mar 23 '07 #3
Small bug: i forgot to set RecreateDynamicButton after first creation in new
button click event handler, should be :

protected void btnNew_Click(object sender, EventArgs e)
{
..
CreateDynamicButton();
RecreateDynamicButton = true;
}

Have a nice evening

--
Milosz
"Milosz Skalecki [MCAD]" wrote:
Howdy,

Dynamically created controls are not persited bewteen postback. You have to
recreate them go make postback event handler fire:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && RecreateDynamicButton)
{
CreateDynamicButton();
}
}

private void CreateDynamicButton()
{
BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

placeHodler.Controls.Add(bdtbSave);

}

protected void btnNew_Click(object sender, EventArgs e)
{
..
CreateDynamicButton();

}

private bool RecreateDynamicButton
{
get
{
object value = ViewState["RecreateDynamicButton"];
return value == null ? false : (bool) value;
}
set
{
ViewState["RecreateDynamicButton"] = value;
}
}

Hope this helps
--
Milosz
"Mark" wrote:
Hi, I have built a custom control button which inherits from WebControl and
implements IPostBackEventHandler. The control also declares an event Click
and provides a method OnClick which invokes the registered delegate. All is
fine with my control until I try to add it to the page dynamically in a code
behind event handler and wire-up the click event using the normal syntax such
that;

protected void btnNew_Click(object sender, EventArgs e)
{

..

BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

When I do this the PostBack occurs but the event isn’t handled by the
handler I provid, i.e. the event didn’t seem to register.
After reading many posts I thought this may be because I was trying to
register the event too late in the page’s lifecycle. But, as a test I
dynamically added a Button webcontrol alongside my customcontrol and wired-up
an event handler for this which worked!

For further info my control is not a composite control it uses the Render
event to write it’s content using the HtmlTextWriter.

Why doesn’t my custom control’s event get registered when a webcontrol does?
Mar 23 '07 #4
Thanks Milosz!


"Milosz Skalecki [MCAD]" wrote:
Small bug: i forgot to set RecreateDynamicButton after first creation in new
button click event handler, should be :

protected void btnNew_Click(object sender, EventArgs e)
{
..
CreateDynamicButton();
RecreateDynamicButton = true;
}

Have a nice evening

--
Milosz
"Milosz Skalecki [MCAD]" wrote:
Howdy,

Dynamically created controls are not persited bewteen postback. You have to
recreate them go make postback event handler fire:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && RecreateDynamicButton)
{
CreateDynamicButton();
}
}

private void CreateDynamicButton()
{
BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);

placeHodler.Controls.Add(bdtbSave);

}

protected void btnNew_Click(object sender, EventArgs e)
{
..
CreateDynamicButton();

}

private bool RecreateDynamicButton
{
get
{
object value = ViewState["RecreateDynamicButton"];
return value == null ? false : (bool) value;
}
set
{
ViewState["RecreateDynamicButton"] = value;
}
}

Hope this helps
--
Milosz
"Mark" wrote:
Hi, I have built a custom control button which inherits from WebControl and
implements IPostBackEventHandler. The control also declares an event Click
and provides a method OnClick which invokes the registered delegate. All is
fine with my control until I try to add it to the page dynamically in a code
behind event handler and wire-up the click event using the normal syntax such
that;
>
protected void btnNew_Click(object sender, EventArgs e)
{
>
..
>
BDTextButton bdtbSave = new BDTextButton();
..
..
bdtbSave.ID = "btnSave";
bdtbSave.Click += new EventHandler(this.btnSave_OnClick);
>
>
>
When I do this the PostBack occurs but the event isn’t handled by the
handler I provid, i.e. the event didn’t seem to register.
After reading many posts I thought this may be because I was trying to
register the event too late in the page’s lifecycle. But, as a test I
dynamically added a Button webcontrol alongside my customcontrol and wired-up
an event handler for this which worked!
>
For further info my control is not a composite control it uses the Render
event to write it’s content using the HtmlTextWriter.
>
Why doesn’t my custom control’s event get registered when a webcontrol does?
>
>
Mar 28 '07 #5

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

Similar topics

4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
2
by: Juan Romero | last post by:
Hey guys, I am working on a web custom control that basically draws a table (ASP Table) with a few child controls in the cells. I have a command button inside one of these cells. The problem I...
1
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
1
by: tranky | last post by:
Hi boys.. ....i've a question for you! I've a custom control with a button. Well... I need to create that: <mytagprefix:mytag runat="server" onButtonClick="Button_Click"> And in the code...
0
by: OceanBreeze | last post by:
I have added a LinkButton to a table cell programmatically inside the Page_Load method. I also added a custom event to that link button. The same custom event is valid for all the link buttons. The...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.