Hi, I have a CustomValidator control which checks which option the user has selected. On submit, if the decline option is chosen, a check is made to make sure a comment is entered. I have the validation code on both the server and client side. However, nothing seems to happen when I run it up in Firefox. I read this article regarding the <xhtmlConformance mode="Legacy"/>. This didn't seem to be the problem as this option isn't set in my web.config file. Could someone help?
aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function ValidateCommentEntry(oSrc, args)
{
var optConfirm = document.getElementById(oSrc.Confirm);
var optDecline = document.getElementById(oSrc.Decline);
var txtComment = document.getElementById(oSrc.Comment);
if (optDecline.checked)
{
args.IsValid = txtComment.Value != "";
oSrc.innerText = "Please enter a comment.";
}
else if (optConfirm.checked)
{
args.IsValid = true;
}
else
{
oSrc.innerText = "Please select one of the above option.";
args.IsValid = false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server" enableviewstate="false">
<div>
<asp:RadioButton ID="optConfirm" runat="server" GroupName ="ConfirmAction"
Text="I confirm that the above details are correct:"
EnableViewState="False" />
<br />
<asp:RadioButton ID="optDecline" runat="server" GroupName ="ConfirmAction"
Text="The above details are NOT correct:"
EnableViewState="False" />
<br />
Please provide comments or contact your sales rep using the details above:
<br />
<asp:TextBox class="indented" ID="txtComment" runat="server"
Rows="5" Wrap="true" TextMode="MultiLine" Width="600" EnableViewState="False"
MaxLength="4000"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:CustomValidator ID="cvComment"
runat="server"
Display="Dynamic"
EnableClientScript ="true"
OnServerValidate="cvComment_ServerValidate"
ClientValidationFunction="ValidateCommentEntry"></asp:CustomValidator>
</div>
</form>
</body>
</html>
aspx.cs code:
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = DateTime.Now.ToString();
this.AddAttrToCustomValidator();
}
protected void cvComment_ServerValidate(object source, ServerValidateEventArgs args)
{
if (this.optDecline.Checked)
{
this.cvComment.ErrorMessage = "Please enter a comment.";
args.IsValid = this.txtComment.Text.Length > 0;
}
else if (this.optConfirm.Checked)
{
args.IsValid = true;
}
else
{
this.cvComment.ErrorMessage = "Please select one of the above option.";
args.IsValid = false;
}
}
private void AddAttrToCustomValidator()
{
this.Page.ClientScript.RegisterExpandoAttribute(this.cvComment.ClientID, "Confirm", this.optConfirm.ClientID, false);
this.Page.ClientScript.RegisterExpandoAttribute(this.cvComment.ClientID, "Decline", this.optDecline.ClientID, false);
this.Page.ClientScript.RegisterExpandoAttribute(this.cvComment.ClientID, "Comment", this.txtComment.ClientID, false);
}
}