adding user controls to web page at runtime

Discussion in 'ASP .Net Web Controls' started by Sam Kuehn, Nov 19, 2004.

  1. Sam Kuehn

    Sam Kuehn Guest

    There has been a lot of articles on how to load user controls at runtime in the Init() method.
    UserControl myControl = (UserControl)LoadControl(stringControl);
    I add the control in the Init() method so that the viewstate to works properly. What if I want to let the user select what control to display? For instance say I have a drop down list of user controls and want to load the control the user has selected? When the user selects a control the form will auto postback. But at the point Init() method is fired the users selection in the on change event handler for the drop down list has not been set.
    So here is what is happening (I think):
    Init()
    {
    //this method adds control to form
    AddUserControl(SessionManager.Instance.SomeValue);
    }
    ddl_IndexChanged()
    {
    //Add to session so that is available tot he Init() method
    SessionManager.Instance.SomeValue = int.Parse(e.Node.DataKey.ToString());
    }
    So when a user changes a drop down list Init() is called and THEN (the problem) the session variable that tells the AddUserControl() method what control to load is set. So if I were to reload the page without changing the drop down the second time the page would display correctly. Basically the control loaded is always one postback "behind" what the user has selected. Is there any solutions to this problem? I have a lot of instances where it would be nice to load a user control on the fly and I might not know at design time what control I want to load. It my be dictated by user input. I am sure there is a best practices way to accomplish this. It seems like a problem that many people would have run into.
    Many thanks in advance!
     
    Sam Kuehn, Nov 19, 2004
    #1
    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. =?Utf-8?B?TGluZGEgV2llbmhvbHQ=?=

    Adding User Controls at Runtime

    =?Utf-8?B?TGluZGEgV2llbmhvbHQ=?=, Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Hans Kesting
    Feb 9, 2004
  2. Sam Kuehn

    adding user controls at runtime

    Sam Kuehn, Nov 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    527
    Sam Kuehn
    Nov 19, 2004
  3. Mantorok

    Adding user controls at runtime

    Mantorok, May 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    424
    Mantorok
    May 23, 2005
  4. Replies:
    4
    Views:
    434
    Patrice
    Mar 30, 2006
  5. error in adding user controls at runtime

    , Mar 30, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    195
    Patrice
    Mar 30, 2006
Loading...

Share This Page