473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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 1635
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Bailey | last post by:
I am trying to populate a label on a web form Let’s say I have the following select statement “Select Wonum, Location, Priority, Equipment from Workorderâ€
1
by: ahmed | last post by:
hi .. i am trying to make a webform where i can create label with out actually going to the toolbox and dargging the thing over the webform in design mode. Is there a way to do that i tried...
3
by: Jurjen de Groot | last post by:
I'm experimenting with StyleSheets in ASP.NET. If I put a label on my webform and <LINK> the CSS file with the following entries to the webform it doesn't work. It only works if I fill the...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: nospamjac | last post by:
Hi, Is there a way to update the text of an asp:label on a webform without refreshing the entire page? What is called by button clicks and other events that refresh a webform control? See the...
8
by: david | last post by:
I have developed a web form by using visual Studio. My question is: (1) what is the problem? (2) what is right way to do it? In the form, there are labels with id: lblWear, lblColor, and...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
3
by: Anoop M | last post by:
Whether there is any available libraries to render an asp.net webform dynamically at run time, from an xml definition? Like <Page> <control class="System.Web.UI.WebControls.Textbox"...
1
by: Brian Simmons | last post by:
I'll try to keep this short & sweet and hopefully get the direction I'm looking for. I'll drop a SqlDatasource onto a webform. Configure it to use a stored proc, perhaps have the parameters be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.