Chapter 9 Alert Box pg. 305 Item 8
After I set the ShowMessagebox to true and the Show Summary to false the the message box does not display when there are errors on the page. I have included the code for the control and the page. Thank you for your assistance.
C# code from the Control
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtPhoneHome.Text != string.Empty || txtPhoneBusiness.Text != string.Empty)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Control Code Source
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="Controls_ContactForm" %>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 314px;
}
</style>
<script type="text/javascript">
function ValidatePhoneNumbers(source, args)
{
var txtPhoneHome = document.getElementById('<%= txtPhoneHome.ClientID %>');
var txtBusinessPhone = document.getElementById('<%= txtPhoneBusiness.ClientID %>');
if (txtPhoneNumber.value !=' ' || txtPhoneBusiness.Value != ' ')
{
args.IsValid = true;
}
else
{
args.IsValid = false;
{
}
</script>
<table class="style1">
<tr>
<td colspan="3">
<h1>Get in touch with us</h1><p>Use the form below to get in touch with us.
Enter your name, e-mail address and your home or business phone number so we may contact you.</p></td>
</tr>
<tr>
<td style="font-weight: 700">
Your name</td>
<td class="style2">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="Please enter your name">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Your e-mail address"
Font-Bold="True"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtEmailAddress" Display="Dynamic"
ErrorMessage="Please enter an e-mail address">*</asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmailAddress" Display="Dynamic"
ErrorMessage="Please enter a valid e-mail address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Font-Bold="True"
Text="Your e-mail address again"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtEmailAddressConfirm" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
Display="Dynamic" ErrorMessage="Please confirm the e-mail address"
ControlToValidate="txtEmailAddressConfirm">*</asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic"
ErrorMessage="Please re-type the e-mail address"
ControlToCompare="txtEmailAddress" ControlToValidate="txtEmailAddressConfirm">*</asp:CompareValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Font-Bold="True"
Text="Your home phone number"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtPhoneHome" runat="server"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="ValidatePhoneNumbers" Display="Dynamic"
ErrorMessage="Please enter your home or business phone number"
onservervalidate="CustomValidator1_ServerValidate" >*</asp:CustomValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Font-Bold="True"
Text="Business phone number"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtPhoneBusiness" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Font-Bold="True" Text="Comments"></asp:Label>
</td>
<td class="style2" colspan="1">
<asp:TextBox ID="txtComments" runat="server" Height="50px"
TextMode="MultiLine" Width="350px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
Display="Dynamic" ErrorMessage="Please enter a comment"
ControlToValidate="txtComments">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<asp:Button ID="btnSend" runat="server" Font-Bold="True" Text="Send" />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Please correct the following errors before you press the send button"
ShowMessageBox="True" ShowSummary="False" />
</td>
</tr>
</table>
|