Dynamic Control Loading Problem

Discussion in 'ASP .Net Web Controls' started by Kirsta Lemieux, Sep 7, 2004.

  1. Hello,
    I have been working on a dynamically generated control,
    that after traveling through a maze of problems I finally
    reached the end, only to find a really serious one that is
    keeping me awake at night. This is a pretty complex
    problem, which I have been trying to solve for days,
    looking at it from different angles, and no luck, so this
    is my last resort before giving up. Here is the
    description what I have done:

    I have built a UserControl (an ascx file), that has
    multiple dropdown boxes, and based on the value change of
    one of them, the other dropdown boxes (their list of
    values) reload based on the current selected information.
    The way that I do that is detect whether the SelectedIndex
    of the Dropdown box changed (in the PageLoad section). The
    reason why I'm doing this in the PageLoad is because from
    the selected information, I dynamically add other controls
    to the webform (or my control), so that's why I need all
    that before my page is rendered. This all works great,
    just the way I want it. So the way I tested it, is created
    a WebForm, and then dragged and dropped my control. And
    when I go to my created page it works great...

    But now here comes the tricky part. In my other page,
    based on some specific event, I want to DYNAMICALLY LOAD
    my control. So I used the Page.LoadControl
    ("MyControl.ascx") when I detect the event (also in
    PageLoad). Ok, so I tested it, and the control loads fine
    on the event. But then when I change the selection in one
    of my dropdown boxes, at the PageLoad I can no longer
    retrieve the SelectedIndex (it is still unselected at that
    time). Basically as if the the Form State is not loaded in
    at that time (but when I dragged and dropped it in, it did
    load before reaching my code in the PageLoad). Any Idea
    why is that happening?

    If what I wrote above is not clear enough, I included some
    sample steps that you can try it out to reproduce the
    problem:

    PART A
    Create a new ASCX file and call it TestControl.ascx
    In There:
    Add a DropDownList to the Form, and call it lstTest.
    Add a Label to the Form, and call it Label1.
    Set the lstTest.AutoPostback to True
    In the Code Section of the ASCX file:

    Public Sub Page_Load(...standard stuff)
    If lstTest.SelectedIndex > 0 Then
    Label1.Text = "Proves that it Works".
    End If

    If not IsPostBack Then
    lstTest.Items.Add("Test Value 1")
    lstTest.Items.Add("Test Value 2")
    lstTest.Items.Add("Test Value 3")
    End If

    End Sub


    PART B
    Create a new ASPX File and call it Test.aspx

    Method 1 (Drag and Drop):
    Simply Drag and drop the TestControl.ascx to the Test.aspx
    webform. When you run it, and select "Test Value 3", you
    will notice that that Label text changes to: "Proves that
    it Works". And that's exactly what I want. But then:

    Method 2 (Dynamic Loading)
    Instead of dragging and dropping the TestControl.ascx, add
    a PlaceHolder control to the webform and call it
    PlaceHolder1. Once you have done that, add the following
    in the Page_Load section of the Test.aspx code:

    Dim aControl As UserControl = Page.LoadControl
    ("TestControl.ascx")
    aControl.ID = "MyTestControl1"
    PlaceHolder1.Controls.Add(aControl)

    And then run the page. As you see it appears correctly,
    and the dropdown box is loaded. But then when you
    select "Test Value 3", when the page is Loaded by
    AutoPostback the text of the Label does not change, but
    the "Test Value 3" remains selected. If you debug it with
    Visual Studio and break at the line: "If
    lstTest.SelectedIndex > 0 Then" you will notice that the
    selected index is still 0 (and it should be 2).

    Any suggestions. This limits a lot of things when trying
    to load controls dynamically.

    Thank you for your help in advance. If you need the sample
    project code, e-mail me at and
    I'll send it to you.

    Merci,
    Kirsta Lemieux
    Kirsta Lemieux, Sep 7, 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. Jo Inferis
    Replies:
    0
    Views:
    390
    Jo Inferis
    Jun 20, 2004
  2. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Dynamic user control loading/initialising

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    396
    Jo Inferis
    Jun 20, 2004
  3. =?Utf-8?B?Y2luZHk=?=
    Replies:
    2
    Views:
    2,585
    Steven Cheng[MSFT]
    Jun 8, 2005
  4. Replies:
    0
    Views:
    2,058
  5. Larry Bud
    Replies:
    1
    Views:
    348
    bruce barker
    Jan 10, 2007
Loading...

Share This Page