Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用vb在asp.net中限制列表框中的项目选择_Javascript_Asp.net - Fatal编程技术网

Javascript 使用vb在asp.net中限制列表框中的项目选择

Javascript 使用vb在asp.net中限制列表框中的项目选择,javascript,asp.net,Javascript,Asp.net,我想使用vb限制asp.net中列表框中的项目选择数量。我试着用javascript。但是,不起作用。需要帮助吗 <asp:ListBox ID="lbprefferedlocation" runat="server" DataSourceID="locations" DataTextField="City_Name" OnSelectedIndexChanged="chkCount(this)" DataValueField="City_Name" Se

我想使用vb限制asp.net中列表框中的项目选择数量。我试着用javascript。但是,不起作用。需要帮助吗

<asp:ListBox ID="lbprefferedlocation" runat="server" DataSourceID="locations" 
                DataTextField="City_Name" OnSelectedIndexChanged="chkCount(this)" DataValueField="City_Name" SelectionMode="Multiple"></asp:ListBox>
            <asp:HiddenField ID="hiddenChkCount" runat="server" />
            <asp:SqlDataSource ID="locations" runat="server" 
                ConnectionString="<%$ ConnectionStrings:JPConnString %>" 
                SelectCommand="SELECT [City_Name] FROM [State_Info]"></asp:SqlDataSource>



function chkCount(obj)
{
    if(obj.checked==true)
    {
        if( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value >= 5 )
        {
            alert('You cannot select more than 5 items.');
            obj.checked=false;
        }
        else
        {
            document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value ) + 1;
        }
    }
    else
    {
        document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value) - 1;
    }
}

功能chkCount(obj)
{
如果(对象检查==真)
{
if(document.getElementById(“”).value>=5)
{
警报('您不能选择超过5项');
检查对象=错误;
}
其他的
{
document.getElementById(“”).value=parseInt(document.getElementById(“”).value)+1;
}
}
其他的
{
document.getElementById(“”).value=parseInt(document.getElementById(“”).value)-1;
}
}

您可以使用
CustomValidator

 <asp:CustomValidator ID="CV_CheckLocationCount" runat="server"
     ValidateEmptyText="true" 
     ClientValidationFunction="CheckLocationCount"
     OnServerValidate="CheckLocationCount" 
     ControlToValidate="lbprefferedlocation"
     EnableClientScript="true" 
     ErrorMessage="Select at least one and at most 5 locations"
     ValidationGroup="VG_SAVE">*
</asp:CustomValidator>

function CheckLocationCount(sender, args){
    var count=$('#<%=lbprefferedlocation.ClientID %> option:selected').length;
    args.IsValid = count > 0 && count <= 5;
} 
*
函数CheckLocationCount(发送方,参数){
变量计数=$('#选项:选定')。长度;

args.IsValid=count>0&&count“CheckLocationCount”不是member@coders_zone:可能是因为您没有在服务器端提供
CheckLocationCount
。您应该始终在客户端和服务器端进行验证。那么,我应该在服务器端写些什么…??@coders\u zone:
受保护的void CheckLocationCounton(对象源,ServerValidateEventArgs args){args.IsValid=lbpreferedLocation.Items.Cast().Where(i=>i.Selected.Count()@coders_zone:
args.IsValid=lbpreferedLocation.Items.Cast(属于ListItem)().Where(函数(i)i.Selected.Count())
function validateListBoxSelectionCount(listbox, minSelected, maxSelected){
    var selected=0;
    if(listbox != null){
        for (var i=0; i<listbox.length; i++){
            if(listbox.options[i].selected){
               selected++; 
               if(selected>maxSelected)break;
            }
        }
    }
   return (selected >= minSelected && selected <= maxSelected);
} 
var listBox  = document.getElementById("<%=lbprefferedlocation.ClientID %>");
args.IsValid = validateListBoxSelectionCount(listBox, 1, 5);