Runatserver

Despite their similarity to common Windows controls and HTML input controls, server controls are considerably different. They run on the server, not on the client. To be more specific, by default, the events and data generated by the user on the client are posted back to the server, and you handle them there, not on the client as you would in a Windows application. The model is robust enough to let you perform many common operations with little or no code. For example, one of the most common operations on Web sites is to create a login/password security page to force users to authenticate before viewing the contents of a site. Most commonly, these pages have two text fields: one for the username that displays the user's entry in plain text, and one for the password, which typically displays asterisks rather than the characters that the user types. They usually have one or more buttons as well (see Figure 5.2 and Listing 5.2).

Figure 5.2: A simple login page

Listing 5.2: HTML Code for Login Form (ch5-1.aspx)

<%@ Page Language="c#" AutoEventWireup="false"

Figure 5.2: A simple login page

Listing 5.2: HTML Code for Login Form (ch5-1.aspx)

<%@ Page Language="c#" AutoEventWireup="false"

Codebehind="ch5-1.aspx.cs" Inherits="CSharpASP.ch5.ch5_1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML>

<META http-equiv="Content-Type" content="text/html; charset=windows-12 52">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs targetSchema">

<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">

<meta content="C#" name="CODE_LANGUAGE"> </HEAD>

<body ms positioning="GridLayout">

<form id=FORM1 method=post runat="server">

<table style="LEFT: 122px; POSITION: absolute; TOP: 106px" height=137 cellSpacing=1 cellPadding=1 width=474 bgColor=#ffffcc border=1> <tr>

<td colSpan=2><asp:label id=Label1 runat="server" Width="127" Height="18" font-size="Small" font-bold="True" font-names="Verdana"

bordercolor="Transparent" borderstyle="None" backcolor="#FFFFC0">User Name:</asp:label><asp:textbox id=txtUserName runat="server"></asp:textbox></td></tr> <tr>

<td colSpan=2>

<asp:label id=Label2 runat="server" Width="127" Height="18"

font-size="Small" font-bold="True" font-names="Verdana"

bordercolor="Transparent" borderstyle="None"

backcolor="#FFFFC0">

Password:

<asp:textbox id=txtPassword runat="server" textmode="Password"> </asp:textbox> </td> </tr> <tr>

<td align=middle width=200>

<asp:button id=cmdCancel runat="server" Text="Cancel" font-bold="True" font-names="Verdana" commandname="cmdCancel" height="2 4" width="104"></asp:button> </td>

<td align=middle width=200>

<asp:button id=cmdOK runat="server" Text="OK" font-bold="True" font-names="Verdana" commandname="cmdOK" height="25" width="101"></asp:button> </td> </tr> </table> </form> </body> </html>

When you run this example, you can enter text into the fields, just like a standard HTML page. When you click one of the buttons, the browser submits the form data. But unlike a standard HTML form, you get the entered data back. By default, the server uses the ViewState hidden control to rebuild the state of the page and send it right back to the browser.

OK, you may not think that's exciting, but here are the steps required to perform the same operation by coding it manually in a classic ASP page:

1. Check to see if the page was reached via a get (first display) or post request.

2. If post, then retrieve the form data from the request.

3. Using embedded code, rebuild the page. For each control, insert the value you just obtained from the post operation to redisplay the values on the client side, for example <input type="text" value= "<%=Request.Form("txtUserName")%>" id="txtUserName">.

4. For each control, create hidden form variables to hold the current field values, such as <input type="hidden" value= "<%=Request.Form("txtUserName")%>" id="txtUserName">.

That's a considerable amount of work. Using ASP.NET, you didn't have to do anything but lay out some controls. But that's not all: You can perform complex input validation without writing any code.

Was this article helpful?

0 0

Post a comment