Ok, rethinking the fact that dynamic controls do not preserve themselves
when the page is submitted, you must recreate your dynamic controls on each
postback, early in the page lifecycle, such as during the Page_Init event
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" <alessandrozifiglio@NO-SPAM-hotmail.com> wrote in
message news:ImVLb.1434$nC1.287@news.edisontel.com...
[quoted text, click to view] > My apologies dan, I misunderstood the question. What you want to do is
very
> simple. I misread your post. Look at the new code sample. Add it to your
> buttons clickevent in your header1 usercontrol.
>
> 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" <dandaman@test.com> wrote in message
> news:u75scmu1DHA.2460@TK2MSFTNGP10.phx.gbl...
> > Thanks for the suggestion, but I'm trying to use querystrings as a last
> > resort. Any other ideas?
> >
> > Dan
> >
> > "Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> wrote in
> > message news:wcnJb.22943$i_5.18228@news.edisontel.com...
> > > hi dan,
> > > a much better solution would be to have a controlID in your
querystring.
> > > This way you use a normal hyperlink control and pass a different id
for
> > > every
> > > link that is click and then retrieve it on your default page and
> structure
> > > out from there in page_load--this way you can load controls from the
> > default
> > > page itself.
> > >
> > > 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" <dandaman@test.com> wrote in message
> > > news:OuVnSfX0DHA.2032@TK2MSFTNGP09.phx.gbl...
> > > > I have an asp.net page default.aspx with a user control and a
> > placeholder
> > > > control.
> > > >
> > > > <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
> > > >
> > > >
> > >
> > >
> >
> >
>
>