【问题标题】:validate checkbox asp.net c#验证复选框asp.net c#
【发布时间】:2012-06-20 06:28:08
【问题描述】:

我正在尝试验证两个复选框。必须检查其中之一才能使表格有效。我想使用 CustomValidator 控件,并在服务器上进行验证。

(此 .ascx 页面是显示在不同 .aspx 页面上的表单。)

首先,我在 .ascx 页面上添加了复选框和 CustomValidator 控件。像这样:

<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>

然后,我在 .ascx.cs 页面的代码隐藏中添加了 validateCheckBoxes_ServerValidate 函数,如下所示:

            protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (!EmailCourse.Checked && !SpecialReport.Checked)
                args.IsValid = false;
            else
                args.IsValid = true;

    }

当我尝试在本地站点上打开使用此表单的页面以查看其外观时,我收到一个错误,如下所示:

描述:在编译服务此请求所需的资源期间发生错误。请查看以下具体错误详情并适当修改您的源代码。

编译器错误消息:CS1061:“ASP.common_controls_specialreportform_ascx”不包含“validateCheckBoxes_ServerValidate”的定义,并且没有扩展方法“validateCheckBoxes_ServerValidate”接受类型的第一个参数 可以找到“ASP.common_controls_specialreportform_ascx”(您是否缺少 using 指令或程序集引用?)

还有:

错误 CS1061:“ASP.common_controls_specialreportform_ascx”不包含“validateCheckBoxes_ServerValidate”的定义,并且找不到接受“ASP.common_controls_specialreportform_ascx”类型的第一个参数的扩展方法“validateCheckBoxes_ServerValidate”(您是否缺少 using 指令或汇编参考?)

有谁知道这个错误的原因是什么?我是 asp.net 的新手,遇到了麻烦。

谢谢!

【问题讨论】:

  • 您确定代码隐藏处理程序实际上在 ASCX 页面所引用的类中吗?
  • @freefaller 是对的,对我来说也像是类引用或命名空间问题。
  • harry180 在他的回答中的好点...一个错字和复选框也在 ASCX 页面上?

标签: asp.net validation checkbox


【解决方案1】:

您将 validateCheckBoxes_ServerValidate 放在 *.ascx.cs 中,而它应该在您的 aspx.cs 上。 在 ascx.cs 上,您不能像这样引用它在 Parent 上的控件。

将此代码放入您的 aspx.cs 文件中:

protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
        if (!EmailCourse.Checked && !SpecialReport.Checked)
            args.IsValid = false;
        else
            args.IsValid = true;

}

编辑:

您在 ascx 上的自定义验证器应如下所示:

<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="No checkbox  checked" ControlToValidate="EmailCourse" OnServerValidate="validateCheckBoxes_ServerValidate"/>

没有这个ControlToValidate属性服务器不知道你想验证哪个控件。

编辑2:

您是否尝试将&lt;input type="checkbox"/&gt; 更改为&lt;asp:CheckBox /&gt;? 并告诉我在 btn 单击或选中/取消选中复选框后应该如何验证?

编辑3:

检查您的 .ascx.designer.cs EmailCourse 中的类型是否正确。

编辑4:

当您的*.ascx 文件中有&lt;asp:CheckBox .../&gt; 时,您的ascx.designer.cs 中应该有 这种类型protected global::System.Web.UI.WebControls.CheckBox EmailCourse

如果有帮助,请告诉我。

【讨论】:

  • 感谢您的帮助!实际上,我有一个错字,我在原始问题中进行了编辑。复选框位于 .ascx 页面上,validateCheckBoxes_ServerValidate 函数位于 .ascx.cs 页面的代码隐藏中。您认为是什么问题阻止了它的工作?
  • 我尝试过 但我得到了asp.net 运行时错误:“基类包含字段 EmailCourse...但其类型...不兼容”谢谢 Harry180 - 感谢您的帮助!
  • 我在 .ascx.designer.cs 中的 EmailCourse 类型是:protected global::System.Web.UI.HtmlControls.HtmlInputCheckBox EmailCourse;这对我来说看起来不错......
  • 有帮助吗?如果是,请将此作为答案
【解决方案2】:

Qs 你的问题似乎已经回答了,我想告诉你如何才能少写

protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (!EmailCourse.Checked && !SpecialReport.Checked);
}

这和你写的一样,只是在一行中

【讨论】:

    【解决方案3】:

    我已经做了一个你尝试做的工作示例@user1463201

    这里是.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ValidationExample._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></title>
       </head>
       <body>
          <form id="form1" runat="server">
            <div>
              <asp:ValidationSummary runat="server" ID="myValidationSummary" ValidationGroup="validation"/>
               <table>
                 <tr>
                    <td>
                      <asp:CheckBox runat="server" ID="cbxEmailCourse" Text="Email course" EnableViewState="True" AutoPostBack="True"/>
                  </td>
                </tr>
                <tr>
                  <td>
                    <asp:CheckBox runat="server" ID="cbxSpecialReport" Text="Special report"/>
                  </td>
               </tr>
               <tr>
                  <td>
                    <asp:TextBox Visible="False" Text="t" runat="server" ID="txtValid" ValidationGroup="validation"></asp:TextBox>
                  </td>
                  <td>
                    <asp:Button runat="server" ID="btnValid" Text="Validate form" ValidationGroup="validation" OnClick="btnValid_Click"/>
                    <asp:CustomValidator runat="server" ID="cValidator" ControlToValidate="txtValid" ValidationGroup="validation" OnServerValidate="cValidator_Validate"></asp:CustomValidator>
                  </td>
              </tr>
          </table>
       </div>
      </form>
      </body>
     </html>
    

    和 aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ValidationExample
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void cValidator_Validate(object source, ServerValidateEventArgs args)
            {
                args.IsValid = cbxEmailCourse.Checked && cbxSpecialReport.Checked;
            }
    
            protected void btnValid_Click(object sender, EventArgs e)
            {
                if (!Page.IsValid)
                   Response.Write("Page are not Validate");
            }
    
        }
     }
    

    我希望我的工作能帮助别人:) 享受

    【讨论】:

      【解决方案4】:

      好的。所以这是有效的:

      正如 harry180 所建议的,我确实需要将 input-type="CheckBox" 切换为 asp:CheckBox。

      这触发了一个运行时错误,我在上面对此进行了评论。运行时出错是因为我修改后没有重新编译解决方案,为了修改ascs.designer.cs文件。

      重新编译后,代码可以工作了。

      【讨论】:

      • 有谁知道为什么页面需要这么长时间才能验证复选框?我想可能是因为这不在客户端,所以我也添加了客户端验证。这无助于缩短时间。似乎它没有进行我添加的客户端验证。这可以吗?它首先进行服务器端验证吗?那会很奇怪。还是我的客户端验证代码错误?谢谢!
      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      相关资源
      最近更新 更多