article

Introduction to Web Controls in ASP.NET using the label control

Email
Submitted on: 1/1/2015 2:14:00 AM
By: Shane Bauer (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: VB.NET, ASP.NET
Views: 535
 
     This is to give you an introduction to the idea of controls by using the label control.

 
				



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" runat="server" />

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! 

<script runat="server">

Sub page_load
lblexample.text = "Welcome to my site!"
End Sub

</script>

<html>
<head>
<title>My first label</title>
</head>
<body>
<asp:Label id="lblexample" runat="server" />
</body>
</html>

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 site!".

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 me at webmaster@sourcecode-central.com. I will be glad to help you!

 


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.