How to add programmatically a label into a content page?

Discussion in 'ASP .Net' started by Phil, Mar 8, 2007.

  1. Phil

    Phil Guest

    Hi,

    How to add programmatically a label into a content page?
    I tried this but doesn't work:

    Thanks
    Phil


    content page:
    ------------
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    </asp:Content>

    code-behind:
    -----------
    dim lb as label
    lb = New Label
    Content1.controls.add("lb")
     
    Phil, Mar 8, 2007
    #1
    1. Advertising

  2. Phil

    xke Guest

    It won't work that way. Add another control inside
    ContentPlaceHolder1, a table, a panel ...
    Let's talk code:

    YourPage.aspx:

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <asp:panel ID="Panel1" runat="server"></asp:panel>
    </asp:Content>

    YourPage.aspx.cs or YourPage.aspx.vb

    On Page_Load

    C#
    Label lbl = new Label();
    lbl.Text = "This is me!";
    Panel1.Controls.Add(lbl);

    VB.NET
    dim lbl as new Label()
    lbl.Text = "This is me!"
    Panel1.Controls.Add(lbl)


    Let me know if it's not working.



    On Mar 8, 5:31 pm, "Phil" <> wrote:
    > Hi,
    >
    > How to add programmatically a label into a content page?
    > I tried this but doesn't work:
    >
    > Thanks
    > Phil
    >
    > content page:
    > ------------
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > </asp:Content>
    >
    > code-behind:
    > -----------
    > dim lb as label
    > lb = New Label
    > Content1.controls.add("lb")
     
    xke, Mar 9, 2007
    #2
    1. Advertising

  3. Phil

    Phil Guest

    Hi xke, thanks for replying ...

    i get an error: Name 'Panel1' is not declared

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim lbl As New Label()
    lbl.Text = "This is me!"
    Panel1.Controls.Add(lbl)
    End Sub
    End Class


    aspx:
    ----
    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
    AutoEventWireup="false" CodeFile="addlabelcontent.aspx.vb"
    Inherits="addlabelcontent" title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    Runat="Server">
    <asp:panel ID="Panel1" runat="server" Height="50px" Width="125px">
    </asp:panel>
    </asp:Content>







    "xke" <> schreef in bericht
    news:...
    > It won't work that way. Add another control inside
    > ContentPlaceHolder1, a table, a panel ...
    > Let's talk code:
    >
    > YourPage.aspx:
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <asp:panel ID="Panel1" runat="server"></asp:panel>
    > </asp:Content>
    >
    > YourPage.aspx.cs or YourPage.aspx.vb
    >
    > On Page_Load
    >
    > C#
    > Label lbl = new Label();
    > lbl.Text = "This is me!";
    > Panel1.Controls.Add(lbl);
    >
    > VB.NET
    > dim lbl as new Label()
    > lbl.Text = "This is me!"
    > Panel1.Controls.Add(lbl)
    >
    >
    > Let me know if it's not working.
    >
    >
    >
    > On Mar 8, 5:31 pm, "Phil" <> wrote:
    >> Hi,
    >>
    >> How to add programmatically a label into a content page?
    >> I tried this but doesn't work:
    >>
    >> Thanks
    >> Phil
    >>
    >> content page:
    >> ------------
    >> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    >> Runat="Server">
    >> </asp:Content>
    >>
    >> code-behind:
    >> -----------
    >> dim lb as label
    >> lb = New Label
    >> Content1.controls.add("lb")

    >
    >
     
    Phil, Mar 9, 2007
    #3
  4. Phil

    Phil Guest

    Sorry, it works ... now !?!


    "xke" <> schreef in bericht
    news:...
    > It won't work that way. Add another control inside
    > ContentPlaceHolder1, a table, a panel ...
    > Let's talk code:
    >
    > YourPage.aspx:
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > Runat="Server">
    > <asp:panel ID="Panel1" runat="server"></asp:panel>
    > </asp:Content>
    >
    > YourPage.aspx.cs or YourPage.aspx.vb
    >
    > On Page_Load
    >
    > C#
    > Label lbl = new Label();
    > lbl.Text = "This is me!";
    > Panel1.Controls.Add(lbl);
    >
    > VB.NET
    > dim lbl as new Label()
    > lbl.Text = "This is me!"
    > Panel1.Controls.Add(lbl)
    >
    >
    > Let me know if it's not working.
    >
    >
    >
    > On Mar 8, 5:31 pm, "Phil" <> wrote:
    >> Hi,
    >>
    >> How to add programmatically a label into a content page?
    >> I tried this but doesn't work:
    >>
    >> Thanks
    >> Phil
    >>
    >> content page:
    >> ------------
    >> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    >> Runat="Server">
    >> </asp:Content>
    >>
    >> code-behind:
    >> -----------
    >> dim lb as label
    >> lb = New Label
    >> Content1.controls.add("lb")

    >
    >
     
    Phil, Mar 9, 2007
    #4
  5. Phil

    xke Guest

    glad to hear it!


    On Mar 9, 5:27 am, "Phil" <> wrote:
    > Sorry, it works ... now !?!
    >
    > "xke" <> schreef in berichtnews:...
    >
    >
    >
    > > It won't work that way. Add another control inside
    > > ContentPlaceHolder1, a table, a panel ...
    > > Let's talk code:

    >
    > > YourPage.aspx:

    >
    > > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > > Runat="Server">
    > > <asp:panel ID="Panel1" runat="server"></asp:panel>
    > > </asp:Content>

    >
    > > YourPage.aspx.cs or YourPage.aspx.vb

    >
    > > On Page_Load

    >
    > > C#
    > > Label lbl = new Label();
    > > lbl.Text = "This is me!";
    > > Panel1.Controls.Add(lbl);

    >
    > > VB.NET
    > > dim lbl as new Label()
    > > lbl.Text = "This is me!"
    > > Panel1.Controls.Add(lbl)

    >
    > > Let me know if it's not working.

    >
    > > On Mar 8, 5:31 pm, "Phil" <> wrote:
    > >> Hi,

    >
    > >> How to add programmatically a label into a content page?
    > >> I tried this but doesn't work:

    >
    > >> Thanks
    > >> Phil

    >
    > >> content page:
    > >> ------------
    > >> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    > >> Runat="Server">
    > >> </asp:Content>

    >
    > >> code-behind:
    > >> -----------
    > >> dim lb as label
    > >> lb = New Label
    > >> Content1.controls.add("lb")- Hide quoted text -

    >
    > - Show quoted text -
     
    xke, Mar 9, 2007
    #5
    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. John
    Replies:
    1
    Views:
    2,501
    Ken Cox
    Dec 1, 2005
  2. hazz
    Replies:
    6
    Views:
    50,021
    SkyUCHC
    Jun 9, 2010
  3. Dave L
    Replies:
    3
    Views:
    3,364
    Göran Andersson
    Mar 4, 2010
  4. Replies:
    1
    Views:
    334
    John B. Matthews
    Sep 21, 2012
  5. Knut Krueger
    Replies:
    8
    Views:
    325
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page