How to add programmatically a label into a content page?

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

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:
 
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.
 
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 said:
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.
 
Sorry, it works ... now !?!


xke said:
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.
 
Back
Top