spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp_net2 / 1 To page 1To page 2current pageTo page 4
[previous] [next]

Java Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 2.

Server-Side Comments

Server-side comments allow you to include, within the page, comments or notes that will not be processed by ASP.NET. Traditional HTML uses the <!-- and --> character sequences to delimit comments; anything found within these will not be displayed to the user by the browser. ASP.NET comments look very similar, but use the sequences <%-- and --%>.

Our ASP.NET example contains the following server-side comment block:

<%-- Declare the title as string and set it --%>

The difference between ASP.NET comments and HTML comments is that ASP.NET comments are not sent to the client at all. Don’t use HTML comments to try and comment out ASP.NET code. Consider the following example:

<!--
<button runat="server" id="myButton" onServerClick="Click">Click 
Me</button>
<% Title = "New Title" %>
-->

Here, it looks as if a developer has attempted to use an HTML comment to hide not only an HTML button control, but a code render block as well. Unfortunately, HTML comments will only hide things from the browser, not the ASP.NET runtime. So, in this case, while we won’t see anything in the browser that represents these two lines, they will, in fact, have been processed by ASP.NET, and the value of the variable Title will be changed to New Title. The code could be modified to use server-side comments very simply:

<%-- 
<button runat="server" id="myButton" onServerClick="Click">Click 
Me</button>
<% Title = "New Title" %>
--%>  

Now, the ASP.NET runtime will ignore the contents of this comment, and the value of the Title variable will not be changed.

Server-Side Include Directives

Server-side include directives enable developers to insert the contents of an external file anywhere within an ASP.NET page. In the past, developers used server-side includes when inserting connection strings, constants, and other code that was generally repeated throughout the entire site.

There are two ways your server-side includes can indicate the external file to include: using either the file or the virtual attribute. If we use file, we specify its filename as the physical path on the server, either as an absolute path starting from a drive letter, or as a path relative to the current file. Below, we see a file server-side include with a relative path:

<!-- #INCLUDE file="myinclude.aspx" -->

virtual server-side includes, on the other hand, specify the file’s location on the Website, either with an absolute path from the root of the site, or with a path relative to the current page. The example below uses an absolute virtual path:

<!-- #INCLUDE virtual="/directory1/myinclude.aspx" -->

Note that although server-side includes are still supported by ASP.NET, they have been replaced by a more robust and flexible model known as user controls. Discussed in Chapter 16, Rich Controls and User Controls, user controls allow for developers to create a separate page or module that can be inserted into any page within an ASP.NET application.

Literal Text and HTML Tags

The final element of an ASP.NET page is plain old text and HTML . Generally, you cannot do without these elements, and HTML is the means for displaying the information from your ASP.NET controls and code in a way that’s suitable for the user. Returning to the example in Figure 2.1 one more time, let’s focus on the literal text and HTML tags:

<%@ Page Language="VB" %>
<html>
<head>
<title>Sample Page</title>

<script runat="server">
Sub ShowMessage(s As Object, e As EventArgs)
  lblMessage.Text = "Hello World"
End Sub
</script>

</head>
<body>

<form runat="server">
<%-- Declare the title as string and set it --%>
<asp:Label id="lblMessage" runat="server" />
<% Dim Title As String = "Zak Ruvalcaba's Book List" %>
<%= Title %>
</form>

</body>
</html>

As you can see in the bold code, literal text and HTML tags provide the structure for presenting our dynamic data. Without them, there would be no format to the page, and the browser would be unable to understand it.

Now you should understand what the structure of an ASP.NET page looks like. As you work through the examples in this book, you’ll begin to realize that in many cases you won’t need to use all these elements. For the most part, all of your development will be modularized within code declaration blocks. All of the dynamic portions of your pages will be contained within code render blocks or controls located inside a <form runat="server"> tag.

In the following sections, we’ll outline the various languages used within ASP.NET, talk a little about view state, and look at working with directives in more detail.

View State

As I mentioned briefly in the previous section, ASP.NET controls automatically retain their data when a page is sent to the server by a user clicking a submit button. Microsoft calls this persistence of data view state. In the past, developers would have to hack a way to remember the item selected in a drop-down menu or keep the contents of a text box, typically using a hidden form field. This is no longer the case; ASP.NET pages, once submitted to the server for processing, automatically retain all information contained within text boxes, items selected within drop-down menus, radio buttons, and check boxes. Even better, they keep dynamically generated tags, controls, and text. Consider the following ASP page, called sample.asp:

<html>
<head>
  <title>Sample Page using VBScript</title>
</head>
<body>
<form method="post" action="sample.asp">
  <input type="text" name="txtName"/>
  <input type="Submit" name="btnSubmit" text="Click Me"/>
<%
If Request.Form("txtName") <> "" Then
  Response.Write(Request.Form("txtName"))
End If
%>

</form>
</body>
</html>

If you save this example in the WebDocs subdirectory of wwwroot that you created in Chapter 1, Introduction to .NET and ASP.NET, you can open it in your browser by typing http://localhost/WebDocs/sample.asp, to see that view state is not automatically preserved. When the user submits the form, the information that was previously typed into the text box is cleared, although it is still available in Request.Form("txtName"). The equivalent page in ASP.NET, ViewState.aspx, demonstrates data persistence using view state:

Example 2.1. ViewState.aspx

<html>
<head>
<title>Sample Page using VB.NET</title>
<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
  lblMessage.Text = txtName.Text
End Sub
</script>
</head>

<body>
<form runat="server">
  <asp:TextBox id="txtName" runat="server" />
  <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click" 
      runat="server" />
  <asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

Example 2.2. ViewState.aspx

<html>
<head>
<title>Sample Page using C#</title>
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
  lblMessage.Text = txtName.Text;
}
</script>
</head>

<body>
<form runat="server">
  <asp:TextBox id="txtName" runat="server" />
  <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click"
      runat="server" />
  <asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

In this case, the code uses ASP.NET controls with the runat="server" attribute. As you can see in Figure 2.2, the text from the box appears on the page when the button is clicked, but also notice that the data remains in the text box! The data in this example is preserved because of view state:

Figure 2.2. ASP.NET supports view state. When a page is submitted, the information within the controls is preserved.

ASP.NET supports view state. When a page is submitted, the information within the controls is preserved.

You can see the benefits of view state already. But where is all that information stored? ASP.NET pages maintain view state by encrypting the data within a hidden form field. View the source of the page after you’ve submitted the form, and look for the following code:

<input type="hidden" name="__VIEWSTATE" value="dDwtMTcyOTAyO
DAwNzt0PDtsPGk8Mj47PjtsPHQ8O2w8aTwzPjs+O2w8dDxwPGw8aW5uZXJodG
1sOz47bDxIZWxsbyBXb3JsZDs+Pjs7Pjs+Pjs+Pjs+d2wl7GlhgweO9LlUihS
FaGxk6t4=" />

This is a standard HTML hidden form field with the value set to the encrypted data from the form element. As soon as you submit the form for processing, all information relevant to the view state of the page is stored within this hidden form field.

View state is enabled for every page by default. If you do not intend to use view state, you can turn it off, which will result in a slight performance gain in your pages. To do this, set the EnableViewState property of the Page directive to false:

<%@ Page EnableViewState="False" %>

Speaking of directives, it’s time we took a closer look at these curious beasts!


home / programming / asp_net2 / 1 To page 1To page 2current pageTo page 4
[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: March 27, 2003
Revised: June 28, 2004

URL: http://webreference.com/programming/asp_net2/1