Web Controls are server-side elements used to create
"smart forms" in ASP.NET. These Web controls actually turn into
html elements when they are processed on the server. In this article, I am
going to explain how to use the label control. My goal is to teach you
step-by-step by keeping it simple. Instead of throwing all web controls together in one
article, I am going to break it down so you don't get too much at one time.
Ready? Let's begin!
The "label" Control
Adding static content to your page has always been easy.
But what if you want to dynamically change what will be displayed? You
could use the "label" control. Here is the code:
<asp:Label id="lblexample" runat="server"
This is the minimal amount of code you need to create a
label. If you wanted to set a default text value in the label, you would
do something like this:
<asp:Label id="lblexample" text="This is my first label"
Lets break some of this down for a minute. The "asp:Label"
part is the start of the control. The "id" parameter is going to be the
name of your label control. You can name yours whatever you want. In the
text parameter, it set a default text value to "This is my first label."
Do not forget the "runat=server" part. When you execute your page, it
will display "This is my first label."
If you wanted to change the text dynamically in a subroutine, you would
call the following:
lblexample.text = "New text would go here!"
Remember, if you did not call your label "lblexample" then
you would have to change the tag to the appropriate name.
Are you ready for your first example? Of course you are!
lblexample.text = "Welcome to my site!"
<title>My first label</title>
<asp:Label id="lblexample" runat="server" />
Let me explain this now. We started off the script
by using "<script runat="server">." This starts are
server-side scripting section. We will hide our subroutines in there. In
the next line, we declare our first (and only) subroutine. This subroutine
is called "page_load." This subroutine will always be executed when the page
begins to load. For your own reference, you can always use the "page_load"
sub in any of your pages. It will always execute first. The "page_load"
subroutine is a good place to declare and store variables which you will use
throughout the script.
In the next line, we called "lblexample.text." This will
change the text value in the control named "lblexample" to "Welcome to my
After that, we just clean up by ending the "page_load" sub
and closing the script tag. This allows us to start developing what the
user will see when he/she comes to the page. Notice, the rest is all HTML
tags except for the one label control entitled "lblexample." This is the spot
where the "Welcome to my site!" text will be placed. Simple isn't it?
I hope you found that somewhat helpful. I tried to
keep it as simple as possible but still not making it boring. Please vote
and leave your comments! If you have any problems or you get stuck, please email
email@example.com. I will be glad to help you!