Event not firing. Adding controls dynamically to UserControl

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Event not firing. Adding controls dynamically to UserControl

    I am adding controls to the UserControl dynamically and
    then loading the UserControl Dynamically.But I am facing
    problem with firing of click event of one of the buttons
    within the UserControl.It does not fire.
    If I do the same thing in a aspx page instead of a user
    control then the event fires perfectly.

    Adding of dynamic control in the User control is being
    done as follows:

    Page_Load
    {
    ///
    ///
    //
    Table tbl = new Table();
    tbl.Width = Unit.Percentage(100);
    this.Controls.Add(tbl);
    TableRow tr;
    TableCell td;
    btnSkipReg.ImageUrl = "../Images/btn_skip_reg.gif";
    btnSkipReg.ID = "btnSkipReg";
    btnSkipReg.Style.Add("cursor", "hand");
    tr = new TableRow();
    td = new TableCell();
    td.Controls.Add(btnSkipReg);
    tr.Cells.Add(td);
    tbl.Rows.Add(tr);

    ///
    ///

    ///
    }


    private void InitializeComponent()
    {
    this.btnSkipReg.Click += new
    System.Web.UI.ImageClickEventHandler(this.SkipReg_ Click);
    }



    private void SkipRegistration_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
    {

    string redirectURL = "OrderConfUI.aspx";
    Response.Redirect(redirectURL, true);
    }


    nitin Guest

  2. Similar Questions and Discussions

    1. Adding controls dynamically to a datagrid
      Hi, We have a requirement, where based on the UI type selected in a dropdown, a control should get added to the column of a grid dynamically. For...
    2. Dynamically Adding Controls
      I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls...
    3. Event not firing in usercontrol inside usercontrol
      I'm stumped on this problem. I've created a user control that dynamically creates 5 linkbuttons in the CreateChildControls method. Each of these...
    4. dynamically adding user controls
      I have a web form that has a button called "Add Blank Row". Every time this button is pressed a new "blank row" user control should be added to the...
    5. Event not firing.adding dynamic controls to Usercontrol.
      I am adding controls to the UserControl dynamically and then loading the UserControl Dynamically.But I am facing problem with firing of click event...
  3. #2

    Default Re: Event not firing. Adding controls dynamically to UserControl

    Are you dynamically loading and adding the usercontrol at runtime? Or are
    you adding it at design-time? A common problem for controls not firing
    events happens when they're added too late in the control lifecycle (ie.
    after Load).

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    [url]http://obies.com/vga/blog.aspx[/url]
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    and not by private mail.

    "nitin" <nitinsid@rediffmail.com> wrote in message
    news:0aba01c34bab$07b52a30$a601280a@phx.gbl...
    > I am adding controls to the UserControl dynamically and
    > then loading the UserControl Dynamically.But I am facing
    > problem with firing of click event of one of the buttons
    > within the UserControl.It does not fire.
    > If I do the same thing in a aspx page instead of a user
    > control then the event fires perfectly.
    >
    > Adding of dynamic control in the User control is being
    > done as follows:
    >
    > Page_Load
    > {
    > ///
    > ///.> //
    > Table tbl = new Table();
    > tbl.Width = Unit.Percentage(100);
    > this.Controls.Add(tbl);
    > TableRow tr;
    > TableCell td;
    > btnSkipReg.ImageUrl = "../Images/btn_skip_reg.gif";
    > btnSkipReg.ID = "btnSkipReg";
    > btnSkipReg.Style.Add("cursor", "hand");
    > tr = new TableRow();
    > td = new TableCell();
    > td.Controls.Add(btnSkipReg);
    > tr.Cells.Add(td);
    > tbl.Rows.Add(tr);
    >
    > ///
    > ///
    >
    > ///
    > }
    >
    >
    > private void InitializeComponent()
    > {
    > this.btnSkipReg.Click += new
    > System.Web.UI.ImageClickEventHandler(this.SkipReg_ Click);
    > }
    >
    >
    >
    > private void SkipRegistration_Click(object sender,
    > System.Web.UI.ImageClickEventArgs e)
    > {
    >
    > string redirectURL = "OrderConfUI.aspx";
    > Response.Redirect(redirectURL, true);
    > }
    >
    >

    Victor Garcia Aprea [MVP] Guest

  4. #3

    Default Re: Event not firing. Adding controls dynamically to UserControl

    Hi Victor,

    heres the code.

    public class RegistrationUI : CompanyName.UI.Page.PageHost
    {
    protected UCSPGCIRegistration FormControl;

    private void Page_Load(object sender, System.EventArgs e)
    {
    FormControl= (UserControlClassName)LoadControl
    ("FormControl.ascx");
    this.Controls.Add(FormControl);
    }
    }

    Thanks for the help.

    Nitin
    >-----Original Message-----
    >Are you dynamically loading and adding the usercontrol at
    runtime? Or are
    >you adding it at design-time? A common problem for
    controls not firing
    >events happens when they're added too late in the control
    lifecycle (ie.
    >after Load).
    >
    >--
    >Victor Garcia Aprea
    >Microsoft MVP | ASP.NET
    >Looking for insights on ASP.NET? Read my blog:
    >[url]http://obies.com/vga/blog.aspx[/url]
    >To contact me remove 'NOSPAM'. Please post all questions
    to the newsgroup
    >and not by private mail.
    >
    >"nitin" <nitinsid@rediffmail.com> wrote in message
    >news:0aba01c34bab$07b52a30$a601280a@phx.gbl...
    >> I am adding controls to the UserControl dynamically and
    >> then loading the UserControl Dynamically.But I am facing
    >> problem with firing of click event of one of the buttons
    >> within the UserControl.It does not fire.
    >> If I do the same thing in a aspx page instead of a user
    >> control then the event fires perfectly.
    >>
    >> Adding of dynamic control in the User control is being
    >> done as follows:
    >>
    >> Page_Load
    >> {
    >> ///
    >> ///.> //
    >> Table tbl = new Table();
    >> tbl.Width = Unit.Percentage(100);
    >> this.Controls.Add(tbl);
    >> TableRow tr;
    >> TableCell td;
    >> btnSkipReg.ImageUrl = "../Images/btn_skip_reg.gif";
    >> btnSkipReg.ID = "btnSkipReg";
    >> btnSkipReg.Style.Add("cursor", "hand");
    >> tr = new TableRow();
    >> td = new TableCell();
    >> td.Controls.Add(btnSkipReg);
    >> tr.Cells.Add(td);
    >> tbl.Rows.Add(tr);
    >>
    >> ///
    >> ///
    >>
    >> ///
    >> }
    >>
    >>
    >> private void InitializeComponent()
    >> {
    >> this.btnSkipReg.Click += new
    >> System.Web.UI.ImageClickEventHandler
    (this.SkipReg_Click);
    >> }
    >>
    >>
    >>
    >> private void SkipRegistration_Click(object sender,
    >> System.Web.UI.ImageClickEventArgs e)
    >> {
    >>
    >> string redirectURL = "OrderConfUI.aspx";
    >> Response.Redirect(redirectURL, true);
    >> }
    >>
    >>
    >
    >
    >.
    >
    nitin Guest

  5. #4

    Default Re: Event not firing. Adding controls dynamically to UserControl

    Hi Nitin,

    Did you solved this?

    Try changing the loading code to Init instead of Load and let me know if the
    problem still persists,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    [url]http://obies.com/vga/blog.aspx[/url]
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    and not by private mail.

    "nitin" <nitinsid@rediffmail.com> wrote in message
    news:08ac01c34c32$00adc5d0$a101280a@phx.gbl...
    > Hi Victor,
    >
    > heres the code.
    >
    > public class RegistrationUI : CompanyName.UI.Page.PageHost
    > {
    > protected UCSPGCIRegistration FormControl;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > FormControl= (UserControlClassName)LoadControl
    > ("FormControl.ascx");
    > this.Controls.Add(FormControl);
    > }
    > }
    >
    > Thanks for the help.
    >
    > Nitin
    >
    > >-----Original Message-----
    > >Are you dynamically loading and adding the usercontrol at
    > runtime? Or are
    > >you adding it at design-time? A common problem for
    > controls not firing
    > >events happens when they're added too late in the control
    > lifecycle (ie.
    > >after Load).
    > >
    > >--
    > >Victor Garcia Aprea
    > >Microsoft MVP | ASP.NET
    > >Looking for insights on ASP.NET? Read my blog:
    > >[url]http://obies.com/vga/blog.aspx[/url]
    > >To contact me remove 'NOSPAM'. Please post all questions
    > to the newsgroup
    > >and not by private mail.
    > >
    > >"nitin" <nitinsid@rediffmail.com> wrote in message
    > >news:0aba01c34bab$07b52a30$a601280a@phx.gbl...
    > >> I am adding controls to the UserControl dynamically and
    > >> then loading the UserControl Dynamically.But I am facing
    > >> problem with firing of click event of one of the buttons
    > >> within the UserControl.It does not fire.
    > >> If I do the same thing in a aspx page instead of a user
    > >> control then the event fires perfectly.
    > >>
    > >> Adding of dynamic control in the User control is being
    > >> done as follows:
    > >>
    > >> Page_Load
    > >> {
    > >> ///
    > >> ///.> //
    > >> Table tbl = new Table();
    > >> tbl.Width = Unit.Percentage(100);
    > >> this.Controls.Add(tbl);
    > >> TableRow tr;
    > >> TableCell td;
    > >> btnSkipReg.ImageUrl = "../Images/btn_skip_reg.gif";
    > >> btnSkipReg.ID = "btnSkipReg";
    > >> btnSkipReg.Style.Add("cursor", "hand");
    > >> tr = new TableRow();
    > >> td = new TableCell();
    > >> td.Controls.Add(btnSkipReg);
    > >> tr.Cells.Add(td);
    > >> tbl.Rows.Add(tr);
    > >>
    > >> ///
    > >> ///
    > >>
    > >> ///
    > >> }
    > >>
    > >>
    > >> private void InitializeComponent()
    > >> {
    > >> this.btnSkipReg.Click += new
    > >> System.Web.UI.ImageClickEventHandler
    > (this.SkipReg_Click);
    > >> }
    > >>
    > >>
    > >>
    > >> private void SkipRegistration_Click(object sender,
    > >> System.Web.UI.ImageClickEventArgs e)
    > >> {
    > >>
    > >> string redirectURL = "OrderConfUI.aspx";
    > >> Response.Redirect(redirectURL, true);
    > >> }
    > >>
    > >>
    > >
    > >
    > >.
    > >

    Victor Garcia Aprea [MVP] Guest

  6. #5

    Default Re: Event not firing. Adding controls dynamically to UserControl

    Hi,

    This got solved.I had to set the usercontrol's ID while loading the user
    control, and it worked.

    Thanks for the help anyway.

    Regards,
    Nitin Khungar



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    nitin khungar Guest

  7. #6

    Default Re: Event not firing. Adding controls dynamically to UserControl

    Can you post what the code looks like now? I'm having a similar problem.



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Patrick Sullivan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139