Web Form Controls

Most Web development involves interacting with user data in one way or another. Much of this data is contained in forms that users have filled out. Other items that fall into the user data category are passed as parameters or contained in session variables (see Chapter 7 for in-depth coverage of session-state and application-state topics). Web development in the past has always dealt with these kinds of user data. The ASP.NET Web Form controls provide a new way to interact with this data.

ASP programming can be used to create ASP pages that contain both HTML and server-side VBScript code. The HTML is static it doesn't change whereas the server-side VBScript code dynamically generates HTML that is sent to the client machine. When this type of ASP programming was first introduced, it offered a powerful way to quickly and easily develop Web applications. The problem with this approach, of course, is that alternating between HTML and ASP code is confusing, and the code is difficult to follow and decipher. Classic ASP is much like the fabled spaghetti code that was so common in early BASIC programs.

Classic ASP development also offers some challenges that are not so obvious. For instance, what if you have a list of items in a <select> object? You might have 10 or 15 objects, perhaps that describe merchandise colors. Depending on the situation, such as a previously chosen user selection, or some item that may be on sale, you might want different items in the list to be selected. For instance, if red sweaters are on sale this week, you might want to default the color selector to red. Maybe your company has an abundance of red sweaters, and by making red the default, you feel you can sell more red sweaters. So you have to decide which of the items in the selection list is designated when the page first appears to the user. In classic HTML, you do this by adding the selected attribute to one of the option items. And if you are creating static HTML, this is easy to do. But when you need to make a dynamic decision on which item to add this attribute to, things become more complicated. What you end up doing is including a conditional test in the code, and then outputting the selected attribute to the appropriate item. Although developers have used this " tried and true" method over the past five years, it tends to lead to unreadable and possibly even unmanageable code.

The following classic ASP code in Listing 2.1 shows the exact situation I just described. For your benefit, I have included the resulting HTML code in Listing 2.2.

Listing 2.1 Classic ASP Code That Dynamically Selects a Color
 <select size="1" name="Colors">   <option value=Red>Red</option>   <option value=Green>Green</option>   <option value=Blue>Blue</option> <%   If SESSION( "User" ) = "JOHN" Then     Response.Write( "<option selected value=Magenta>Magenta</option>" )     Response.Write( "<option value=Orange>Orange</option>" )     Response.Write( "<option value=Teal>Teal</option>" )   ElseIf SESSION( "User" ) = "GEORGE" Then     Response.Write( "<option value=Magenta>Magenta</option>" )     Response.Write( "<option selected value=Orange>Orange</option>" )     Response.Write( "<option value=Teal>Teal</option>" )   Else     Response.Write( "<option value=Magenta>Magenta</option>" )     Response.Write( "<option value=Orange>Orange</option>" )     Response.Write( "<option selected value=Teal>Teal</option>" )   End If %> </select> 
Listing 2.2 The Result of Listing 2.1 When the User Is GEORGE
 <select size="1" name="Colors">   <option value=Red>Red</option>   <option value=Green>Green</option>   <option value=Blue>Blue</option>   <option value=Magenta>Magenta</option>   <option selected value=Orange>Orange</option>   <option value=Teal>Teal</option> </select> 

With ASP.NET, the preceding ASP code can be replaced with much simpler code that is easier to organize. First, a DropDownList Web Form control is added to the Web Form (more about doing this in the section entitled "Placing Controls" later in the chapter). The option values must be added, and the resulting .aspx code is shown in Listing 2.3.

Listing 2.3 A DropDownList Control Alternative to an HTML Select
 <asp:DropDownList  runat="server">   <asp:ListItem Value="Red">Red</asp:ListItem>   <asp:ListItem Value="Green">Green</asp:ListItem>   <asp:ListItem Value="Blue">Blue</asp:ListItem>   <asp:ListItem Value="Magenta">Magenta</asp:ListItem>   <asp:ListItem Value="Teal">Teal</asp:ListItem> </asp:DropDownList> 

Now, to simply select one of the items, you can use the code shown in Listing 2.4.

Listing 2.4 The Code to Select an Item when Using DropDownList Control Code

C#

 if( Session["User"] == "JOHN" ) {   DropDownList1.SelectedIndex = 0; } else if( Session["User"] == "GEORGE" ) {   DropDownList1.SelectedIndex = 1; } else {   DropDownList1.SelectedIndex = 2; } 

OR

 switch (Session["User"] ) {     case "JOHN":       DropDownList1.SelectedIndex = 0;         break;     case "George":       DropDownList1.SelectedIndex = 1;         break;     default:       DropDownList1.SelectedIndex = 2;         break; } 

VB

 If Session("User") = "JOHN" Then   DropDownList1.SelectedIndex = 0 ElseIf Session("User") = "GEORGE" Then   DropDownList1.SelectedIndex = 1 Else   DropDownList1.SelectedIndex = 2 End If 

OR

 Select Case Session("Test")  Case "John"   DropDownList1.SelectedIndex = 0  Case "George"   DropDownList1.SelectedIndex = 1  Case Else   DropDownList1.SelectedIndex = 2 End Select 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net