Event handling of dynamically loaded user control

Discussion in 'ASP .Net' started by JB McMichael, Aug 2, 2005.

  1. JB McMichael

    JB McMichael Guest

    I have an aspx page that based on a query loads a user control dynamically.

    The aspx page is simple, just a place holder, the code behind queries a
    table that stores the names of the ascx files. I then add the control
    to the placeholder:

    Dim myUC As Control = Page.LoadControl("includes/" & ascx_page)
    myplaceholder.Controls.Add(myUC)

    Each of these user controls has 2 or 3 textboxes and a Submit button.
    Each user control has a onclick method for the Submit button in which
    the values of the text boxes are entered in to a database. My problem
    is that the event never fires. The code compiles, and the forms load up
    just fine, but clicking on the Submit button does nothing. So now I am
    stuck and looking for some help.
     
    JB McMichael, Aug 2, 2005
    #1
    1. Advertising

  2. JB McMichael

    Stefan Guest

    The timing is very important when adding controls dynamically.
    Typically you should add your controls in the Init method of the page

    If you do it later (e.g. on PreRender), your viewstate gets corrupted.
    Corrupted viewstate is the #1 reason for events not firing.
     
    Stefan, Aug 2, 2005
    #2
    1. Advertising

  3. JB McMichael

    JB McMichael Guest

    Stefan wrote:
    > The timing is very important when adding controls dynamically.
    > Typically you should add your controls in the Init method of the page
    >
    > If you do it later (e.g. on PreRender), your viewstate gets corrupted.
    > Corrupted viewstate is the #1 reason for events not firing.
    >


    I am adding my controls in the page load. Is there a way to prevent the
    viewstate from getting corrupted?
     
    JB McMichael, Aug 2, 2005
    #3
  4. JB McMichael

    Stefan Guest

    See the code sample on denisbauer.com for dynamic control injecting in
    Page_Load
     
    Stefan, Aug 4, 2005
    #4
    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. Thanh Nguyen
    Replies:
    0
    Views:
    463
    Thanh Nguyen
    Apr 1, 2004
  2. Tonix
    Replies:
    0
    Views:
    434
    Tonix
    Aug 24, 2004
  3. =?Utf-8?B?U3RldmUgQm9vdGg=?=

    Event fails to fire first time in dynamically loaded user control(

    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    624
    =?Utf-8?B?U3RldmUgQm9vdGg=?=
    Apr 6, 2005
  4. Replies:
    1
    Views:
    1,072
  5. Tonix
    Replies:
    0
    Views:
    192
    Tonix
    Aug 24, 2004
Loading...

Share This Page