您好我正在使用自定义验证器对复选框选中状态验证文本框..
如果复选框选中false并且textbox为空,则应抛出类似于所需的CertID的错误...我在此方法中放置了断点" cvCertId_OnServerValidate"当我点击提交按钮时,它根本没有击中......
但是当我点击提交按钮时,我无法收到错误..
这是我的aspx代码
<div class="SearchLine">
<div class="Firstlbl">
<asp:Label ID="Lbl1" runat="server" Text=" CertificateID *" Font-Bold="True"></asp:Label>
</div>
<div class="firstTxtBox">
<asp:TextBox ID="txtCertificateID" runat="server" Width="300px"></asp:TextBox>
</div>
<div class="rfvValidation">
<asp:CustomValidator ID="cvCertId" runat="Server" OnServerValidate="cvCertId_OnServerValidate" Display="Dynamic" ValidationGroup="Validate" CssClass="validationerror"></asp:CustomValidator>
</div>
</div>
<div class="SearchLine">
<div class="chkboxLine">
<asp:CheckBox ID="chkSerialNumber" runat="server" AutoPostBack="True" OnCheckedChanged="chkSerialNumber_CheckedChanged" />
</div>
<div class="Firstlbl">
<asp:Label ID="Lbl2" runat="server" Text="Serial Number *" Font-Bold="True"></asp:Label>
</div>
<div class="firstTxtBox">
<asp:TextBox ID="txtCertificateSerialNumber" runat="server" Width="300px" Enabled="false"></asp:TextBox>
</div>
<div class="rfvValidation">
<asp:CustomValidator ID="cvSerialNumber" runat="Server" OnServerValidate="cvSerialNumber_OnServerValidate" Display="dynamic" ValidationGroup="Validate" CssClass="validationerror"></asp:CustomValidator>
</div>
</div>
<div class="SearchResetControls">
<asp:Button ID="btnSubmit" runat="server" Text="Re-Assign"
onclick="btnSubmit_Click" ValidationGroup="Validate"/>
</div>
<div runat="server" id="divMessage">
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
这是我的aspx.cs代码
protected void cvCertId_OnServerValidate(object sender, ServerValidateEventArgs args)
{
if (chkSerialNumber.Checked == false)
{
string certID = txtCertificateID.Text;
if (certID == string.Empty)
{
((CustomValidator)sender).ErrorMessage = "Required Certificate Id";
args.IsValid = false;
}
}
}
protected void cvSerialNumber_OnServerValidate(object sender, ServerValidateEventArgs args)
{
if (chkSerialNumber.Checked == true && string.IsNullOrEmpty(txtCertificateSerialNumber.Text))
{
((CustomValidator)sender).ErrorMessage = "Required Certificate Serial Number";
args.IsValid = false;
}
}
请有人帮忙吗
提前多多感谢....
答案 0 :(得分:0)
您需要在自定义验证控件中删除ControlToValidate属性。并添加文本和ErrorMessage属性。例如,
<div class="firstTxtBox">
<asp:TextBox ID="txtCertificateSerialNumber" runat="server" Width="300px" Enabled="false"></asp:TextBox>
</div>
<div class="rfvValidation">
<asp:CustomValidator ID="cvSerialNumber" runat="Server" OnServerValidate="cvSerialNumber_OnServerValidate" Display="dynamic" ValidationGroup="Validate" CssClass="validationerror" Text="Error" ErrorMessage="This is error message"></asp:CustomValidator>
</div>