Custom control's click event

G

Guest

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

Guest

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
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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?
 
G

Guest

Thanks Milosz!




Milosz Skalecki said:
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 said:
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 said:
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?
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top