UserControl Events Not Firing with Dynamically Created Button Control

Discussion in 'ASP .Net' started by jasinx@gmail.com, May 4, 2007.

  1. Guest

    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 -------------------------------------------
     
    , May 4, 2007
    #1
    1. Advertising

  2. gover Guest

    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)
    <> wrote in message
    news:...
    > 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 -------------------------------------------
    >
     
    gover, May 5, 2007
    #2
    1. Advertising

  3. gover Guest

    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" <> wrote in message
    news:%...
    > 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)
    > <> wrote in message
    > news:...
    >> 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 -------------------------------------------
    >>

    >
    >
     
    gover, May 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    728
  2. Mark Rae
    Replies:
    3
    Views:
    568
    =?Utf-8?B?amlt?=
    Sep 24, 2005
  3. SevDer
    Replies:
    2
    Views:
    1,305
    Walter Wang [MSFT]
    Nov 13, 2007
  4. Robert Sheppard

    Dynamically Created Controls Not Firing Events

    Robert Sheppard, Oct 28, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    258
    Prodip Saha
    Oct 28, 2005
  5. tapuleo
    Replies:
    12
    Views:
    207
    David Mark
    Aug 2, 2007
Loading...

Share This Page