验证复选框asp.net C# [英] validate checkbox asp.net c#
问题描述
我想验证两个复选框。其中之一必须按顺序检查的形式是有效的。我想用一个CustomValidator控件,并验证在服务器上。
I am trying to validate two checkboxes. One of them must be checked in order for the form to be valid. I would like to use a CustomValidator control, and validate on the server.
(此的.ascx页]是显示不同的.aspx页上的形式。)
(This .ascx page is a form that is displayed on a different .aspx page.)
首先,我把在复选框和我的.ascx页面上的CustomValidator控件。像这样的:
First I put in the checkboxes and a CustomValidator control on my .ascx page. Like this:
<tr>
<td colspan="3">
<input type="checkbox" runat="server" name="EmailCourse" class="" id="EmailCourse" value="" />
Email course
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="No checkbox checked"
OnServerValidate="validateCheckBoxes_ServerValidate">
</asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="3">
<input type="checkbox" runat="server" name="SpecialReport" class="" id="SpecialReport" value="" />
Special report
</td>
</tr>
然后,我说在$ C $的validateCheckBoxes_ServerValidate功能C-背后的.ascx.cs页面上,像这样的:
Then, I added the validateCheckBoxes_ServerValidate function in the code-behind, on the .ascx.cs page, like this:
protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
if (!EmailCourse.Checked && !SpecialReport.Checked)
args.IsValid = false;
else
args.IsValid = true;
}
当我尝试打开使用此表单上我的本地网站,看看它是什么样子,我得到一个错误,像这样的页面:
When I try to open the page that uses this form on my local site to see what it looks like, I get an error, like this:
说明:该请求提供服务所需资源的编译过程中出现错误。请检查下列特定错误详细信息并适当地修改源$ C $ C。
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
编译器错误信息:CS1061:'ASP.common_controls_specialreportform_ascx'不包含'validateCheckBoxes_ServerValidate的定义,并没有扩展方法'validateCheckBoxes_ServerValidate接受式的第一个参数
ASP.common_controls_specialreportform_ascx'可以找到(是否缺少using指令或程序集引用?)
Compiler Error Message: CS1061: 'ASP.common_controls_specialreportform_ascx' does not contain a definition for 'validateCheckBoxes_ServerValidate' and no extension method 'validateCheckBoxes_ServerValidate' accepting a first argument of type 'ASP.common_controls_specialreportform_ascx' could be found (are you missing a using directive or an assembly reference?)
和
错误CS1061:'ASP.common_controls_specialreportform_ascx'不包含'validateCheckBoxes_ServerValidate的定义,并没有扩展方法'validateCheckBoxes_ServerValidate接受型ASP.common_controls_specialreportform_ascx'的第一个参数可以找到(是否缺少using指令或程序集引用?)
error CS1061: 'ASP.common_controls_specialreportform_ascx' does not contain a definition for 'validateCheckBoxes_ServerValidate' and no extension method 'validateCheckBoxes_ServerValidate' accepting a first argument of type 'ASP.common_controls_specialreportform_ascx' could be found (are you missing a using directive or an assembly reference?)
有谁知道这个错误的原因是什么?我在asp.net新的和我有这个麻烦了。
Does anyone know what the cause of this error is? I'm new at asp.net and am having trouble with this.
谢谢!
推荐答案
确定。因此,这里的工作是什么:
OK. So here's what worked:
由于harry180建议,我也需要切换输入型=复选框到ASP:复选框。
As harry180 suggested, I did need to switch the input-type="CheckBox" to asp:CheckBox.
这引发了运行时错误,这是我评论了关于以上。运行时错误是因为我还没有重新编译进行更改,以使ascs.designer.cs文件被修改后的溶液。
This triggered a run-time error, which I commented about above. The run-time error was because I had not recompiled the solution after making the change, in order that the ascs.designer.cs file be modified.
重新编译后,code ++工程。
After recompiling, the code works.
谢谢,harry180!你是了不起的!
Thanks, harry180! You're amazing!
这篇关于验证复选框asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!