简体   繁体   English

如何验证将从数据库值生成的复选框列表

[英]how to validate checkboxlist that will generate from the database values

There is no validation controle for checkboxlist in asp.net ,C#. C#中的checkboxlist没有验证控件。 Checkbox will be generated from the database values and that will place in the panel. 复选框将根据数据库值生成,并将放置在面板中。 But the problem is I can not validate the checkboxlist. 但是问题是我无法验证复选框列表。 Can someone tell me, how I can solve this problem. 有人可以告诉我如何解决这个问题。

Use a CustomValidator in your code to use ASP.NET to verify. 在代码中使用CustomValidator可以使用ASP.NET进行验证。

<asp:CheckBoxList ID="CheckBoxList1" runat="server"
    ClientID="MyCBL"
    DataSource="datasource"
    DataTextField="display"
    DataValueField="value">
</asp:CheckBoxList>

<asp:CustomValidator ID="CustomValidator1" runat="server"
    ErrorMessage="You must select a value."
    ClientValidationFunction="VerifyCheckBoxList">
</asp:CustomValidator>

Javascript Java脚本

function VerifyCheckBoxList(sender, args) {
    var mycbl = document.getElementById('<%=MyCBL.ClientID%>').value;
    if (...) { // verify your checkbox list here
        args.IsValid = false; // pass false to cancel postback
    }
    else {
        // do your other things
    }
}

Take a look at the MSDN page at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator(v=vs.110).aspx http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.customvalidator(v=vs.110).aspx上查看MSDN页面

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

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