如何在C#中验证多个asp.net复选框
本文关键字:asp net 复选框 验证 | 更新日期: 2023-09-27 17:57:38
我花了很多时间试图在web应用程序中获得4个复选框进行验证。这个web应用程序已经编写好了,并且在可能应该使用复选框列表的时候使用复选框。然而,我相信更改所有现有的代码将是一项艰巨的工作,因此能够验证现有的4个框将为我节省大量的重写时间。
两天来,我一直在stackoverflow中寻找答案,我找到了几个答案,如果我有一个复选框,这些答案会对我有所帮助,但如果我有多个复选框的话,我什么都做不到,我只想验证一个复选盒。
以下是ascx表单的复选框信息:
</asp:Panel>
<cc1:PopupControlExtender ID="PopupControlExtender1" PopupControlID="PanelTypeOwnership"
runat="server" TargetControlID="helpTypeOwnership" Position="Left">
</cc1:PopupControlExtender>
<strong> Type of Ownership:</strong></td>
<td class="style12">
</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=" 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=" Educational" />
</td>
<td class="style12">
<asp:CheckBox ID="chbReligious" runat="server" CssClass="texLabel"
Text=" Religious" />
</td>
<td class="style11">
<asp:CheckBox ID="chbCharitable" runat="server" CssClass="texLabel"
Text=" 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();
}
}
我确信我错过了什么。我仍然可以在不勾选任何框的情况下提交表格。如有任何帮助,我们将不胜感激。
注意:我知道Education拼写错误。我继承了这个网站——我只是还没有抽出时间在所有相关的地方更改它。
另一个选项是在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类名解析多个复选框值,您可以查看此链接