Problems with dynamic LoadControl

A

Andreas

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.
 
G

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
 
A

Andreas

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
 
J

Jose Rodriguez

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top