Listing 71 Post Testaspx a page that generates a postback when the dropdown list

selection is changed selection is changed

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="PostTest.aspx.vb" Inherits="Chapter07_DropDownPost.PostTest"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title></title>

<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content=""> </HEAD> <body>

<form id="Form1" method="post" runat="server"> <table width="600" border="0"> <tr>

<td align="middle">

<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True"> <asp:ListItem Value="Black"> —Select Color—</asp:ListItem> <asp:ListItem Value="Red">Red</asp:ListItem> <asp:ListItem Value="Green">Green</asp:ListItem> <asp:ListItem Value="Blue">Blue</asp:ListItem> </asp:dropdownlist> <br> <br>

<asp:Label id="Label1"

runat="server"></asp:Label> <br> <br> <br> </td> </tr> </table> </form> </body> </HTML>

The code (reformatted slightly for presentation here) creates a simple form, with a dropdown list and a label. The label is initially blank. The drop-down list has an initial value, "—Select Color—", as well as list items for Red, Green, and Blue. In addition to the normal attributes in the asp:dropdownlist tag, AutoPostBack is set to True. The AutoPostBack attribute is available for various controls, including drop-down lists, list boxes, check boxes, and text boxes. When AutoPostBack is set to True in one of these controls and when the control is changed (by selecting an item in a list, changing the value of a check box, and changing the text in a text box), a round-trip is made to the server so that the server can react to changes in the control. Listing 7-2 shows the code-behind file for PostTest.aspx, named PostText.aspx.vb.

0 0

Post a comment