How to add programmatically a label into a content page?

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:
 
X

xke

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.
 
P

Phil

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.
 
P

Phil

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top