<html>
<body>
<form id="myform" method="post" runat="server" />
<PageHeader:Header id="header1" runat="server" />
<asp:PlaceHolder ID="content" runat="server" />
</form>
</body>
</html>
In my user control I have 5 linkbuttons. I would like to have each of these
linkbuttons load a different user control into the placeholder on the
default.aspx page. Is this possible? If so how can I add my user controls
to the placeholder from another user control?
Thanks,
Dan
so in default.aspx page load you can do something like this :
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim controlID As String
Dim c1 As Control
controlID = Request.QueryString("controlID")
Select Case controlID
case "control1"
c1 = LoadControl("my_control_name.ascx")
PlaceHolder1.Controls.Add(c1)
case "control2"
c1 = LoadControl("my_control_name.ascx")
PlaceHolder1.Controls.Add(c1)
case "control3"
c1 = LoadControl("my_control_name.ascx")
PlaceHolder1.Controls.Add(c1)
case "control4"
c1 = LoadControl("my_control_name.ascx")
PlaceHolder1.Controls.Add(c1)
case "control5"
c1 = LoadControl("my_control_name.ascx")
PlaceHolder1.Controls.Add(c1)
case else
Dim lblerror As New Label()
lblerror.Text = "How did you get here ;)"
PlaceHolder1.Controls.Add(lblerror)
End select
End Sub
"Dan" <dand...@test.com> wrote in message
news:OuVnSfX0...@TK2MSFTNGP09.phx.gbl...
Dan
"Alessandro Zifiglio" <alessandr...@NO-SPAM-hotmail.com> wrote in
message news:wcnJb.22943$i_5....@news.edisontel.com...
Dim placeholder1 As PlaceHolder
Dim c1 As Control
c1 = LoadControl("webusercontrol2.ascx")
placeholder1 = Me.Parent.FindControl("placeholder1")
placeholder1.Controls.Add(c1)
What you need to do is simply access your parent control, which is your page
hosting the control and find the control you are looking for, in your case a
placeholder. The find control method takes a string, so pass the id of your
place holder here. Once you got the reference to your place holder, simply
add the new usercontrol to it. However i suggest doing this in the page_load
event of your user control and not the click event of a button. This is
because you are dynamically adding a control to the page and dynamically
added controls need to be rebuilt after postback ;P
"Dan" <dand...@test.com> wrote in message
news:u75scmu1...@TK2MSFTNGP10.phx.gbl...
a possible solution to your problem, that is if you still want to load
controls when a certain button is clicked in your usercontrol is to set a
flag once the control is loaded and then checking for this in the page_init
method of your usercontrol that loaded the control by using viewstate to set
the flag like this :
This will work nicely.
However since I am using viewstate as a flag, viewstate is not available in
page_init --but page_load is a good enough substitute :)
Now your control will be rebuilt even after postback, maintaining state
etc--
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
If Not viewstate("Control1") Is Nothing Then
Load_control1()
End If
End Sub
Private Sub Load_control1()
Dim placeholder1 As PlaceHolder
Dim c1 As Control
c1 = LoadControl("webusercontrol2.ascx")
placeholder1 = Me.Parent.FindControl("placeholder1")
placeholder1.Controls.Add(c1)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Load_control1()
'now set the flag in viewstate
viewstate("Control1") = "Control1"
End Sub
"Alessandro Zifiglio" <alessandr...@NO-SPAM-hotmail.com> wrote in
message news:ImVLb.1434$nC1...@news.edisontel.com...
Dan
"Alessandro Zifiglio" <alessandr...@NO-SPAM-hotmail.com> wrote in
message news:M9YLb.1485$nC1...@news.edisontel.com...