Custom control's click event

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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
 
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
 
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?
 
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?
 
Back
Top