Problems with dynamic LoadControl

Discussion in 'ASP .Net' started by Andreas, Mar 4, 2006.

  1. Andreas

    Andreas Guest

    Hello,

    I have the following problem:

    I want to add to my site (test.aspx) one (1) UserControl
    (customer.ascx) several times - dynamically with a placeholder. As well
    I want to hand off different variables (i.e. Session variable) to the
    UserControl.

    Example:

    Test.aspx:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load


    Session("CustomerLetter") = "A"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))


    Session("CustomerLetter") = "B"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))


    Session("CustomerLetter") = "F"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))


    End Sub


    customer.ascx:


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load


    przGetCustomer(Session("CustomerLetter"))
    get the customer in a Datalist
    End Sub


    Now the problem:
    If I call test.aspx I get in the UserControl (customer.ascx) 3 times
    the customer with the letter "F", and not one with "A", one with "B"
    and one with "F".

    What do I wrong and what can I do?

    Thank you for your help.

    Andreas


    P.S. I try to hand of the variable with Property (Get, Set), but it
    doesn't work also.
     
    Andreas, Mar 4, 2006
    #1
    1. Advertisements

  2. Andreas

    Guest Guest

    Hi Andreas,

    Your code does load the usercontrol three time

    // First load
    Session("CustomerLetter") = "A"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))

    // Second load
    Session("CustomerLetter") = "B"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))

    // Third load
    Session("CustomerLetter") = "F"
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))

    I suppose you should use condition to load it:

    Select (expression)
    Case "A" ' or what ever condition for A
    Session("CustomerLetter") = "A"
    Case "B" ' or what ever condition for B
    Session("CustomerLetter") = "B"
    Case "F" ' or what ever condition for F
    Session("CustomerLetter") = "F"
    End Select
    plcHldTest.Controls.Add(LoadControl("customer.ascx"))


    HTH

    Elton Wang
     
    Guest, Mar 5, 2006
    #2
    1. Advertisements

  3. Andreas

    Andreas Guest

    Hi Elton,

    thanks for the answer.

    But I want to load the usercontrol in fact three times (with different
    customer letters)!

    The problem are the first both variables ("A" and "B") which are not
    recognized. I get the three usercontrols with the letter "F".

    What is wrong?

    Andreas
     
    Andreas, Mar 5, 2006
    #3
  4. Andreas, would this work?


    ctrl = Page.LoadControl("myusercontrol.ascx")
    ctrl.ID = "controlA"
    pnlContainer.Controls.Add(ctrl)

    ctrl = Page.LoadControl("myusercontrol.ascx")
    ctrl.ID = "controlB"
    pnlContainer.Controls.Add(ctrl)

    ctrl = Page.LoadControl("myusercontrol.ascx")
    ctrl.ID = "controlF"
    pnlContainer.Controls.Add(ctrl)

    HTH

    Jose
     
    Jose Rodriguez, Mar 5, 2006
    #4
  5. Andreas

    Guest Guest

    Set a breakpoint in code and trace running to see what happens.
     
    Guest, Mar 5, 2006
    #5
  6. Andreas

    Andreas Guest

    Hello Jose,

    no - it's the same effect.

    Greetings

    Andreas
     
    Andreas, Mar 6, 2006
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.