468,765 Members | 1,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

How put label on a webform dynamically?

I would like to put a label (or any control) on a webform all from the code.
I.e. something like this:

protected void Page_Load(object sender, EventArgs e)
{
Label TestLabel = new Label();
TestLabel.Text = "Hello world";
}

without having to have anything like this already on the page:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

I.e. dynamically build a web page.

Thanks for any info.

Jul 10 '08 #1
3 1509
Nevermind.. got it. Page.Controls.Add

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:5B**********************************@microsof t.com...
>I would like to put a label (or any control) on a webform all from the
code. I.e. something like this:

protected void Page_Load(object sender, EventArgs e)
{
Label TestLabel = new Label();
TestLabel.Text = "Hello world";
}

without having to have anything like this already on the page:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

I.e. dynamically build a web page.

Thanks for any info.
Jul 10 '08 #2
Label Testlabel = new Label();
TestLabel.Text = "Hi there";
Page.Controls.Add(TestLabel);

Usually though, what you want to do is put a PlaceHolder control on the
page. That way you can dynamically add things like a Label into specific
regions.

Keep in mind, when you try to do this with LinkButtons things tend to blow
up in your face because the LinkButton has to be added early enough to catch
any events it fires. Dynamically created controls that have events linked to
them are tricky to master so be wary about them and do a good bit of reading
on dynamically added controls before trying to work with them.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:5B**********************************@microsof t.com...
I would like to put a label (or any control) on a webform all from the
code. I.e. something like this:

protected void Page_Load(object sender, EventArgs e)
{
Label TestLabel = new Label();
TestLabel.Text = "Hello world";
}

without having to have anything like this already on the page:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

I.e. dynamically build a web page.

Thanks for any info.
Jul 10 '08 #3
In addition to the PlaceHolder approach, you can also use ParseControl to
parse a control (or multiple Web server controls) from a string. Google
ParseControl and you'll come up with some examples. This makes it easy to
mix static HTML with Web Server controls and dynamically insert the whole
shootin' match into your page (or PlaceHolder.Controls collection) at
runtime.

- HTH
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:5B**********************************@microsof t.com...
>I would like to put a label (or any control) on a webform all from the
code. I.e. something like this:

protected void Page_Load(object sender, EventArgs e)
{
Label TestLabel = new Label();
TestLabel.Text = "Hello world";
}

without having to have anything like this already on the page:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

I.e. dynamically build a web page.

Thanks for any info.



Jul 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Bailey | last post: by
1 post views Thread by ahmed | last post: by
3 posts views Thread by Jurjen de Groot | last post: by
3 posts views Thread by Anoop M | last post: by
1 post views Thread by Brian Simmons | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.