Add control to asp:PlaceHolder on asp.net page from user control on same page

Discussion in 'ASP .Net Building Controls' started by Dan, Jan 2, 2004.

  1. Dan

    Dan Guest

    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
    Dan, Jan 2, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    >
    Alessandro Zifiglio, Jan 2, 2004
    #2
    1. Advertising

  3. Dan

    Dan Guest

    Thanks for the suggestion, but I'm trying to use querystrings as a last
    resort. Any other ideas?

    Dan

    "Alessandro Zifiglio" <> wrote in
    message news:wcnJb.22943$...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
    Dan, Jan 9, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > Thanks for the suggestion, but I'm trying to use querystrings as a last
    > resort. Any other ideas?
    >
    > Dan
    >
    > "Alessandro Zifiglio" <> wrote in
    > message news:wcnJb.22943$...
    > > 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" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 10, 2004
    #4
  5. 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" <> wrote in
    message news:ImVLb.1434$...
    > 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" <> wrote in message
    > news:...
    > > Thanks for the suggestion, but I'm trying to use querystrings as a last
    > > resort. Any other ideas?
    > >
    > > Dan
    > >
    > > "Alessandro Zifiglio" <> wrote in
    > > message news:wcnJb.22943$...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 10, 2004
    #5
  6. Dan

    Dan Guest

    Thank you very much for the suggestions. I will give them a try.

    Dan

    "Alessandro Zifiglio" <> wrote in
    message news:M9YLb.1485$...
    > 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" <> wrote in
    > message news:ImVLb.1434$...
    > > 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" <> wrote in message
    > > news:...
    > > > Thanks for the suggestion, but I'm trying to use querystrings as a

    last
    > > > resort. Any other ideas?
    > > >
    > > > Dan
    > > >
    > > > "Alessandro Zifiglio" <> wrote

    in
    > > > message news:wcnJb.22943$...
    > > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Dan, Jan 12, 2004
    #6
    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. Dan
    Replies:
    1
    Views:
    5,380
    Christophe Gijbels
    Jan 4, 2004
  2. Carlos Cruz

    Add control to a placeholder

    Carlos Cruz, Apr 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    Ken Cox [Microsoft MVP]
    Apr 12, 2004
  3. David Hubbard
    Replies:
    2
    Views:
    813
    David Hubbard
    Jan 12, 2006
  4. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    321
  5. Dan
    Replies:
    3
    Views:
    785
    Alessandro Zifiglio
    Jan 21, 2004
Loading...

Share This Page