ASP.NET Basics (part 1): Nothing But .Net - Command And Control (Page 5 of 8 )
One bane of ASP programming was the "spaghetti" style of coding, in which HTML and in-line ASP code were often fighting for space and attention. More often than not, maintaining such code was a nightmarish experience, and one best handled after a few strong cups of coffee.
ASP.NET provides a more elegant solution in the form of "server controls". Take a look:
<script language="C#" runat="server">
void Page_Load()
{
quote.Text="Choice. The problem is choice.";
}
</script>
<html>
<head>
<title>Hello, Choice!</title>
</head>
<body>
<asp:label id="quote" runat="server" />
</body>
</html>
Save this as "label.aspx" in your "ASP101" folder and view it in your browser - you should see something like this:

The example above is much easier to read compared to the earlier one, because I've managed to separate the dynamic C# code from the static HTML tags, thereby making it easier to comprehend (and easier to maintain in the long run).
<script language="C#" runat="server">
// snip
</script>
If you're familiar with JavaScript, you already know about the <script> element, which makes it possible to embed JavaScript client-side code within an HTML file. ASP.NET takes this one step further and allows the developer to embed server-side code within an HTML page, as seen in the above code snippet.
The "runat" attribute within the <script> element, which contains the value "server", indicates that the code within the <script> element should be executed on the server and not the client. As earlier, I have to indicate the language using the "language" attribute.
Don't worry too much about the code within the <script> element at the moment, I'll be coming back to it in a moment. Instead, proceed downwards, to the second unfamiliar element in the page:
<asp:label id="quote" runat="server" />
In ASP.NET-lingo, this is called a "server control". Server controls are very similar to the regular HTML elements your normally use, with the main difference being that they are actually created at run-time on the server.
Confused? Take a look at the source code of the page generated by the example above - you'll see that ASP.NET automatically converts the server control into an equivalent <span>:
[output]
...[/output]
Note that the "id" attribute in the rendered version has the same name as the "id" attribute given to the server control in the ASP.NET source code. This "id" is a unique identifier that can be used to refer to the server control in your C# code...as I have done a few lines above:
<script language="C#" runat="server">
void Page_Load()
{
quote.Text="Choice. The problem is choice.";
}
</script>
The code within the Page_Load() function is automatically executed by the Web server when the page is loaded. In this particular example, the code sets the "Text" attribute of the server control named "quote" to the line of text "Choice. The problem is choice."
When the server loads the page, it looks for a server control named "quote" within the page and, on finding it, sets its "Text" attribute to the value specified. The server control is automatically converted in an equivalent HTML element - a <span> - and the value of the "Text" attribute is written inside it.
A number of other attributes of the "label" server control are also available - drop by http://www.gotdotnet.com/quickstart/aspplus/samples/classbrowser/cs/classbro
wser.aspx?namespace=System.Web.UI.WebControls for a complete list
Server controls are an important aspect of programming in ASP.NET. I'll be exploring them in-depth as we move along, I've only included the example above to give you an idea of how they can be used.
Next: Naming Names >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire