繁体   English   中英

如何在C#中验证多个asp.net复选框

[英]How do I validate multiple asp.net checkboxes in C#

我花了很多时间试图获取4个复选框以在Web应用程序中进行验证。 该Web应用程序已被编写,并且在可能应使用复选框列表的情况下使用复选框。 但是,我相信更改所有现有代码将花费大量的工作,因此能够验证现有的4个框将使我完全不必重写。

我已经通过stackoverflow寻找答案了两天,我发现了几个答案,如果我有一个复选框,这将对我有帮助,但是如果我有多个复选框,那么我将无法工作,我只想验证一个单盒。

以下是ascx表单的复选框信息:

                                        </asp:Panel>
                                    <cc1:PopupControlExtender ID="PopupControlExtender1" PopupControlID="PanelTypeOwnership"
                                        runat="server" TargetControlID="helpTypeOwnership" Position="Left">
                                    </cc1:PopupControlExtender>
                                                <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type of Ownership:</strong></td> 
                                            <td class="style12">
                                                &nbsp;</td>
                                            <td class="style11">
                                            </td>
                                            <td>
                                                <strong>
                                                            <div class="textBoxImg" id="helpTypeOwnership" runat="server" style="width: 24px;
                                                                float: right;">
                                                            </div>

                                                </strong>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td style="padding-left: 2px;" class="style2">
                                                    <asp:CheckBox ID="chbAgricultural" runat="server" CssClass="texLabel" Text="&nbsp;&nbsp;Agricultural/Horticultural Society"
                                BorderStyle="None" Width="260px" />
                                            </td>
                                            <asp:CustomValidator runat="server" ID="validateCheckBoxes" EnableClientScript="true"
                                            OnServerValidate="validateCheckBoxes_ServerValidate"
                                            ClientValidationFunction="validateCheckBoxes_ServerValidate">Please select a type of ownership.</asp:CustomValidator>
                                            <td valign="middle" align="left" class="style10">
                            <asp:CheckBox ID="chbEnducational" runat="server" CssClass="texLabel" 
                                Text="&nbsp;&nbsp;Educational" />
                                    </td> 
                                            <td class="style12">
                            <asp:CheckBox ID="chbReligious" runat="server" CssClass="texLabel" 
                                Text="&nbsp;&nbsp;Religious" />
                                            </td>
                                            <td class="style11">
                            <asp:CheckBox ID="chbCharitable" runat="server" CssClass="texLabel" 
                                Text="&nbsp;&nbsp;Charitable" />
                                            </td>
                                        </tr> </table>
                                        <div style="height: 39px;">
                                        </div>

与ascx.cs表单上的复选框验证有关的代码为:

    protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (!chbAgricultural.Checked && !chbEnducational.Checked && !chbReligious.Checked && !chbReligious.Checked)
        args.IsValid = false;
    else
        args.IsValid = true;
}

提交按钮的代码为:

    protected void lbnSubmit_Click(object sender, EventArgs e)              

        {
          if (Page.IsValid)
        {


        MembershipUser mUser = Membership.GetUser(Page.User.Identity.Name);
        if (mUser == null) return;


        DateTime dateT = Convert.ToDateTime(TextBoxDateWrite.Text);
        FairShareApplication451a.changeStatusToVoid(new Guid(mUser.ProviderUserKey.ToString()), GetParcelId());
        FairShareApplication451a apl451a = FairShareApplication451a.CreateApplication451a(new Guid(mUser.ProviderUserKey.ToString()),
               lblNumberApplication.Text, TextBoxCounty.TextBoxText, TextBoxTaxyear.TextBoxText, TextBoxContactName.TextBoxText, TextBoxContactPhone.TextBoxText, TextBoxStateIncorporated.TextBoxText,    //
            GetParcelId(), chbAgricultural.Checked, chbEnducational.Checked, chbReligious.Checked, chbCharitable.Checked, TextBoxOrganizationName.TextBoxText, TextBoxPropOwnerName.TextBoxText,//
            TextBoxMailingAddress.TextBoxText,
            TextBoxCity.TextBoxText, DDListControl2.SelectedValue, TextBoxZipCode.TextBoxText, //TextBoxState.TextBoxText
            TextBoxPropertyDescriptions.TextBoxText,
            TextBoxAuthorizedSignature.Text, TextBoxTitle.Text, dateT, "Not Reviewed",
            PropertyAddress.TextBoxText, PropertyCity.TextBoxText, PropertyState.SelectedValue, PropertyZip.TextBoxText); // Convert.ToDateTime(TextBoxDateWrite.Text)

        //save uploaded files
        SaveUploadedFiles(apl451a.Id);
        FileUploader1.ResetControl();

        MailSend m_snd = new MailSend(Server, Request);
        m_snd.SendMessage(apl451a.UserId, FairShare.MailSend.mailType.received);
        Response.Redirect("~/securezone/CurrentApplications.aspx");
        //       ClearAll();
        }
}

我确定我缺少什么。 我仍然可以提交表格而无需选中任何框。 任何帮助将不胜感激。

注意:我知道教育拼写错误。 我继承了这个网站-我只是还没有在所有相关地方进行更改。

另一个选择是在ASCX用户控件上创建一个属性

public bool IsOneCheckboxChecked
{
 get
 {
  return (chbAgricultural.Checked || chbEnducational.Checked || chbReligious.Checked || chbCharitable.Checked);
 }
}

然后,您可以删除此方法:(并且可以避免在过程中发)

protected void validateCheckBoxes_ServerValidate

何时提交表单,请检查:

if (userControlInstance.IsOneCheckboxChecked) 
{
 // good to go.
}

要检查是否选中了一个或多个复选框,您只需要

args.IsValid = (chbAgricultural.Checked || chbEnducational.Checked || chbReligious.Checked || chbCharitable.Checked)

(您在其中两次有chbReligious )。

而且我非常确定您需要将CustomValidator绑定到控件进行检查,除非页面中没有其他显示的内容。

为什么要放弃客户端验证? 在CustomValidator中,您具有: OnServerValidate =“ validateCheckBoxes_ServerValidate”
ClientValidationFunction =“ validateCheckBoxes_ServerValidate”

您有ClientValidationFunction指向相同的服务器功能。 尝试执行以下操作: ClientValidationFunction =“ validateCheckBoxes_CLIENTValidate”

作为客户端功能是这样的:

<script type="text/jscript">
    function validateCheckBoxes_CLIENTValidate(sender, e) {
        e.IsValid = jQuery(".texLabel input:checkbox").is(':checked');
    }
</script>

如果您无法通过CSS类名称解析多个复选框值,则可以查看此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM