<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Required Field Validation</title>
</head>
<body>
<form runat="server" ID="frmBugs">
<asp:Label ID="lblMsg"
Text="Please report your bug here"
ForeColor="red" Font-Name="Verdana"
Font-Size="10" runat=server />
<ASP:DropDownList id=ddlBooks runat=server>
<asp:ListItem>-- Please Pick A Book --</asp:ListItem>
<asp:ListItem>Programming ASP.NET</asp:ListItem>
<asp:ListItem>Programming .NET Windows Applications</asp:ListItem>
<asp:ListItem>Programming C#</asp:ListItem>
<asp:ListItem>Programming Visual Basic 2005</asp:ListItem>
<asp:ListItem>
Teach Yourself C++ In 21 Days
</asp:ListItem>
<asp:ListItem>
Teach Yourself C++ In 24 Hours
</asp:ListItem>
<asp:ListItem>TY C++ In 10 Minutes</asp:ListItem>
<asp:ListItem>TY More C++ In 21 Days</asp:ListItem>
<asp:ListItem>C++ Unleashed</asp:ListItem>
</ASP:DropDownList>
</td>
<!-- Validator for the drop down -->
<td align=center rowspan=1>
<asp:RequiredFieldValidator
id="reqFieldBooks"
ControlToValidate="ddlBooks"
Display="Static"
InitialValue="-- Please Pick A Book --"
ErrorMessage = "You did not choose a book from the drop-down"
Width="100%" runat=server> * </asp:RequiredFieldValidator>
<td>
<ASP:RadioButtonList id=rblEdition
RepeatLayout="Flow" runat=server>
<asp:ListItem>1st</asp:ListItem>
<asp:ListItem>2nd</asp:ListItem>
<asp:ListItem>3rd</asp:ListItem>
<asp:ListItem>4th</asp:ListItem>
</ASP:RadioButtonList>
</td>
<td align=center rowspan=1>
<asp:RequiredFieldValidator
id="reqFieldEdition"
ControlToValidate="rblEdition"
ErrorMessage = "You did not pick an edition"
Display="Static"
InitialValue=""
Width="100%" runat=server>*</asp:RequiredFieldValidator>
<ASP:TextBox id=txtBug
runat=server
width="183px"
textmode="MultiLine"
height="68px"/>
<asp:RequiredFieldValidator
id="reqFieldBug"
ControlToValidate="txtBug"
ErrorMessage = "You must provide bug details"
Display="Static"
Width="100%" runat=server>*</asp:RequiredFieldValidator>
<asp:DropDownList id="lstDisplay"
AutoPostBack=true
OnSelectedIndexChanged="lstDisplay_SelectedIndexChanged"
runat=server>
<asp:ListItem Selected ="true">Summary</asp:ListItem>
<asp:ListItem>Msg. Box</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="lstFormat"
AutoPostBack=true
OnSelectedIndexChanged="lstFormat_SelectedIndexChanged"
runat=server>
<asp:ListItem>List</asp:ListItem>
<asp:ListItem Selected="true">Bulleted List</asp:ListItem>
<asp:ListItem>Single Paragraph</asp:ListItem>
</asp:DropDownList>
<ASP:Button id=btnSubmit
text="Submit Bug" runat=server OnClick="btnSubmit_Click" />
<asp:ValidationSummary
ID="ValSum" runat="server"
DisplayMode="BulletList"
HeaderText="The following errors were found: " ShowSummary="True" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default_aspx : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMsg.Text = "Page is Valid!";
}
else
{
lblMsg.Text = "Some of the required fields are empty";
}
}
protected void lstFormat_SelectedIndexChanged(object sender, EventArgs e)
{
ValSum.DisplayMode =
(ValidationSummaryDisplayMode)
lstFormat.SelectedIndex;
}
protected void lstDisplay_SelectedIndexChanged(object sender, EventArgs e)
{
ValSum.ShowSummary = lstDisplay.SelectedIndex == 0;
ValSum.ShowMessageBox = lstDisplay.SelectedIndex == 1;
}
}