Listing 69 A Web Form with Several Input Controls ch65aspx

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

Codebehind="ch6-5.aspx.cs" Inherits="CSharpASP.ch6_5" %> <HTML>

<meta name="vs targetSchema"

content=""> <meta name="GENERATOR"

content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> </HEAD>

<body ms_positioning="GridLayout">

<form id="ch6 5" method="post" runat="server">

<asp:TextBox id="txtName" runat="server" Width="165" Height="28" style="LEFT: 262px; POSITION: absolute; TOP: 106px"></asp:TextBox> <asp:Label id="Label3" runat="server" Height="19px"

Width="84px" style="LEFT: 161px; POSITION: absolute; TOP: 110px">Name</asp:Label> <asp:Label id="Label2" runat="server" Width="83" Height="19" style="LEFT: 156px; POSITION: absolute; TOP: 14 6px">Occupation</asp:Label> <asp:DropDownList id="lstOccupation" runat="server"

Width="164px" Height="22px" style="LEFT: 263px; POSITION: absolute; TOP: 146px">

<asp:ListItem Value="">(Select One)</asp:ListItem> <asp:ListItem Value="Programmer">Programmer

</asp:ListItem> <asp:ListItem Value="Analyst">Analyst</asp:ListItem> <asp:ListItem Value="Other">Other</asp:ListItem> </asp:DropDownList>

<asp:RadioButton id="optUnemployed" runat="server" Text="Unemployed" groupname="employmentStatus" style="LEFT: 346px; POSITION: absolute; TOP: 183px"></asp:RadioButton> <asp:RadioButton id="optEmployed" runat="server" Text="Employed" groupname="employmentStatus" style="LEFT: 244px; POSITION: absolute; TOP: 185px"></asp:RadioButton> <asp:Button id="Button1" runat="server" Text="Submit" style="LEFT: 367px; POSITION: absolute; TOP: 217px"></asp:Button>&nbsp;&nbsp;

Load the Web Form ch6-5.aspx into your browser, enter some values, and then click the Submit button. You'll see the names of the various controls and their values appear in the response. Here's the code that displays the values:

private void Button1 Click(object sender, System.EventArgs e) { Response.Write("<b>Form data received</b>" + "<br>"); foreach (String s in Request.Form.AllKeys) {

Response.Write(s + "=" + Request.Form.Get(s) + "<br>");

As with other collections, the AllKeys property returns a String array containing the keys, which in this case are the IDs of the controls—except for the radio buttons, which use the value of the groupname attribute. The browser does not return the names of the grouped radio buttons and does not return any value for unchecked radio buttons. Therefore, code such as the following fragment will not work, because the value isn't present in the form data that the browser sends:

if (Request.Form.Get("optUnemployed") == false) { // Do something

Fortunately, there's a workaround. In the form value display example, the code explicitly excludes the

_viewstate form value because it's present for all Web Forms by default and because the content is meaningless for the present purpose. However, the value does have a purpose—it lets the server compare the initial values of the controls with their present value; that's what raises change events. In other words, by the time the button-click (or any other control event) code runs, ASP.NET has already retrieved the form values and compared them with the initial control states stored in the_viewstate string. Therefore, despite the lack of Request.Form data for the unchecked radio buttons, you can still test their value directly:

if (optUnemployed.Value == false { // Do something

For those of you familiar with classic ASP Web programming, this marks a shift away from using the Response.Form values directly in favor of a control properties model. While you still can use the Response.Form values directly, there's less reason to do so if you let the page maintain Viewstate. It's easier and takes less code to check the control values than to work with the raw Response.Form values.

In this particular instance, there's a second solution. You could use a RadioButtonList Web server control, which groups radio buttons, automatically lets users select only one item from the group, and has a SelectedIndex property that lets you detect which button (if any) a user selected.

Was this article helpful?

0 0

Post a comment