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

时间:2014-04-08 19:40:46

标签: c# asp.net checkbox customvalidator

我花了很多时间试图在Web应用程序中获得4个复选框进行验证。此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();
        }
}

我确信我错过了一些东西。我仍然可以在不查看任何方框的情况下提交表格。任何帮助将不胜感激。

注意:我知道教育拼写错误。我继承了这个网站 - 我在所有相关的地方都没有改变它。

3 个答案:

答案 0 :(得分:2)

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

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

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

protected void validateCheckBoxes_ServerValidate

当提交表格时,请检查:

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

答案 1 :(得分:1)

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

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

(那里有chbReligious两次)。

我相当确定您需要将CustomValidator绑定到Control以进行检查,除非页面中还有其他内容未显示。

答案 2 :(得分:0)

为什么要放弃客户端验证?在您的CustomValidator中,您有: 的 OnServerValidate =&#34; validateCheckBoxes_ServerValidate&#34;
ClientValidationFunction =&#34; validateCheckBoxes_ServerValidate&#34;

您有ClientValidationFunction指向相同的服务器功能。尝试以下内容: 的 ClientValidationFunction =&#34; validateCheckBoxes_CLIENTValidate&#34;

成为客户端功能:

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

如果您无法通过css类名称解析多个复选框值,则可以查看this link