Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > UserControl Events Not Firing with Dynamically Created Button Control

Reply
Thread Tools

UserControl Events Not Firing with Dynamically Created Button Control

 
 
jasinx@gmail.com
Guest
Posts: n/a
 
      05-04-2007
When I run this code it creates a button on an .aspx page. When I
click the button it seems to fire the events but I'm unable to capture
them in the function in the ascx codebehind. Any help? Thank Jason.

start ASCX codebehind -------------------------------------------

public partial class WebUserControl1 : System.Web.UI.UserControl
{

Button btn = new Button();

protected override void OnInit(EventArgs e)
{
btn.Text = "this works";
btn.ID = this.UniqueID + "$btn";
btn.Click += new EventHandler(btn_Click);
base.OnInit(e);
}

protected override void Render(HtmlTextWriter writer)
{

btn.RenderControl(writer);
base.Render(writer);
}

void btn_Click(object sender, EventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}

}

End ASCX codebehind -------------------------------------------

Start ASPX Codebehind -------------------------------------------

public partial class _Default : System.Web.UI.Page
{
WebUserControl1 ctrl;
protected void Page_Load(object sender, EventArgs e)
{
ctrl =
(WebUserControl1)LoadControl("WebUserControl1.ascx ");
ctrl.ID = this.UniqueID;
Page.FindControl("divMain").Controls.Add(ctrl);

}
}

End ASPX Codebehind -------------------------------------------

 
Reply With Quote
 
 
 
 
gover
Guest
Posts: n/a
 
      05-05-2007
I'm quite onclear on this but i remember reading that when you re-create
user on each postback , the events dont get fired because the old button is
gone, and there is a new button created in its place. Try changin the code
to
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(!IsPostBack)
{
btn.Text = "this works";
btn.ID = this.UniqueID + "$btn";
btn.Click += new EventHandler(btn_Click);
}
}
..This won't create a 'new' button each time.
(Just a guess)
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> When I run this code it creates a button on an .aspx page. When I
> click the button it seems to fire the events but I'm unable to capture
> them in the function in the ascx codebehind. Any help? Thank Jason.
>
> start ASCX codebehind -------------------------------------------
>
> public partial class WebUserControl1 : System.Web.UI.UserControl
> {
>
> Button btn = new Button();
>
> protected override void OnInit(EventArgs e)
> {
> btn.Text = "this works";
> btn.ID = this.UniqueID + "$btn";
> btn.Click += new EventHandler(btn_Click);
> base.OnInit(e);
> }
>
> protected override void Render(HtmlTextWriter writer)
> {
>
> btn.RenderControl(writer);
> base.Render(writer);
> }
>
> void btn_Click(object sender, EventArgs e)
> {
> throw new Exception("The method or operation is not
> implemented.");
> }
>
> }
>
> End ASCX codebehind -------------------------------------------
>
> Start ASPX Codebehind -------------------------------------------
>
> public partial class _Default : System.Web.UI.Page
> {
> WebUserControl1 ctrl;
> protected void Page_Load(object sender, EventArgs e)
> {
> ctrl =
> (WebUserControl1)LoadControl("WebUserControl1.ascx ");
> ctrl.ID = this.UniqueID;
> Page.FindControl("divMain").Controls.Add(ctrl);
>
> }
> }
>
> End ASPX Codebehind -------------------------------------------
>



 
Reply With Quote
 
 
 
 
gover
Guest
Posts: n/a
 
      05-06-2007
Hey, One other thing, I think You shold create your controls in
CreateChildControls. If you do it on InInit, you controls will probably be
overwritten. This may help(not sure of the syntax):
protected override void CreateChildControls(EventArgs e)
{
base.CreateChildControls(e);
if(!IsPostBack)
{
btn.Text = "this works";
btn.ID = this.UniqueID + "$btn";
btn.Click += new EventHandler(btn_Click);
}
}

"gover" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I'm quite onclear on this but i remember reading that when you re-create
> user on each postback , the events dont get fired because the old button
> is gone, and there is a new button created in its place. Try changin the
> code to
> protected override void OnInit(EventArgs e)
> {
> base.OnInit(e);
> if(!IsPostBack)
> {
> btn.Text = "this works";
> btn.ID = this.UniqueID + "$btn";
> btn.Click += new EventHandler(btn_Click);
> }
> }
> .This won't create a 'new' button each time.
> (Just a guess)
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> When I run this code it creates a button on an .aspx page. When I
>> click the button it seems to fire the events but I'm unable to capture
>> them in the function in the ascx codebehind. Any help? Thank Jason.
>>
>> start ASCX codebehind -------------------------------------------
>>
>> public partial class WebUserControl1 : System.Web.UI.UserControl
>> {
>>
>> Button btn = new Button();
>>
>> protected override void OnInit(EventArgs e)
>> {
>> btn.Text = "this works";
>> btn.ID = this.UniqueID + "$btn";
>> btn.Click += new EventHandler(btn_Click);
>> base.OnInit(e);
>> }
>>
>> protected override void Render(HtmlTextWriter writer)
>> {
>>
>> btn.RenderControl(writer);
>> base.Render(writer);
>> }
>>
>> void btn_Click(object sender, EventArgs e)
>> {
>> throw new Exception("The method or operation is not
>> implemented.");
>> }
>>
>> }
>>
>> End ASCX codebehind -------------------------------------------
>>
>> Start ASPX Codebehind -------------------------------------------
>>
>> public partial class _Default : System.Web.UI.Page
>> {
>> WebUserControl1 ctrl;
>> protected void Page_Load(object sender, EventArgs e)
>> {
>> ctrl =
>> (WebUserControl1)LoadControl("WebUserControl1.ascx ");
>> ctrl.ID = this.UniqueID;
>> Page.FindControl("divMain").Controls.Add(ctrl);
>>
>> }
>> }
>>
>> End ASPX Codebehind -------------------------------------------
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically created select not firing events in IE, works in other browses tapuleo Javascript 12 08-02-2007 02:52 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Dynamically Created Controls Not Firing Events Robert Sheppard ASP .Net Web Controls 1 10-28-2005 07:30 PM
Dynamically created webcontrol's events not firing Mark Rae ASP .Net 3 09-24-2005 02:16 PM
Dynamically created DropDownList not firing events or persisting viewstate bryanp10@hotmail.com ASP .Net 5 03-11-2005 10:41 PM



Advertisments