C# 中列表框的多项选择

发布于 2024-11-17 19:23:06 字数 293 浏览 3 评论 0原文

我想知道如何允许用户从列表框中只进行 3 个选择。我正在查看这段代码,我认为这存在一些逻辑错误,但我看不出出了什么问题,因为我是新手,请问有人可以吗?指导我或与我分享一些文章,以便我可以解决这个问题,谢谢:)

我的代码

if (listBox1.SelectedIndex <= 4)
     errorProvider1.SetError(listBox1, "Please pick 1.");
else
     errorProvider1.SetError(listBox1, "");

I was wondering how do i allow user to make only make 3 selection from a list box.I am looking at this code,i think there is some logic error to this but i cant see what went wrong as im new to this can someone please guide me or share some article with me so that i can work on this,thank you:)

my code

if (listBox1.SelectedIndex <= 4)
     errorProvider1.SetError(listBox1, "Please pick 1.");
else
     errorProvider1.SetError(listBox1, "");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多彩岁月 2024-11-24 19:23:06
<asp:ListBox SelectionMode="Multiple"></asp:ListBox>  
<asp:ListBox SelectionMode="Multiple"></asp:ListBox>  
胡大本事 2024-11-24 19:23:06

您可以使用 ASP.NET-CustomValidator 为此:

提供客户端验证功能:

function validateSelectionCount(sender, args){
    var listbox = document.getElementById('ListBox1');
    args.IsValid = validateListBoxSelectionCount(listbox, 3, 3);
} 

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);
} 

提供服务器验证功能:

Protected Sub validateSelectionCount(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    Dim count As Int32 = 0
    Dim maxCount As Int32 = 3
    Dim minCount As Int32 = 3
    Dim lb As ListBox = DirectCast(Me.FindControl(DirectCast(source, CustomValidator).ControlToValidate), ListBox)
    For Each item As ListItem In lb.Items
        If item.Selected Then count += 1
        If count > maxCount Then Exit For
    Next
    args.IsValid = (count >= minCount AndAlso count <= maxCount)
End Sub

以及 aspx 部分:

<asp:ListBox ID="ListBox1" CausesValidation="true" ValidationGroup="VG_SAVE" runat="server" CssClass="content" SelectionMode="Multiple"></asp:ListBox>
<asp:CustomValidator ID="CV_SelectionCount" runat="server" ValidateEmptyText="true" ClientValidationFunction="validateSelectionCount" OnServerValidate="validateSelectionCount" ControlToValidate="ListBox1" Display="None" EnableClientScript="true" ErrorMessage="Select 3 items" Style="visibility: hidden" ValidationGroup="VG_SAVE">*</asp:CustomValidator>

You could use an ASP.NET-CustomValidator for this:

Providing a client-validation function:

function validateSelectionCount(sender, args){
    var listbox = document.getElementById('ListBox1');
    args.IsValid = validateListBoxSelectionCount(listbox, 3, 3);
} 

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);
} 

providing a server-validate function:

Protected Sub validateSelectionCount(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    Dim count As Int32 = 0
    Dim maxCount As Int32 = 3
    Dim minCount As Int32 = 3
    Dim lb As ListBox = DirectCast(Me.FindControl(DirectCast(source, CustomValidator).ControlToValidate), ListBox)
    For Each item As ListItem In lb.Items
        If item.Selected Then count += 1
        If count > maxCount Then Exit For
    Next
    args.IsValid = (count >= minCount AndAlso count <= maxCount)
End Sub

and the aspx part:

<asp:ListBox ID="ListBox1" CausesValidation="true" ValidationGroup="VG_SAVE" runat="server" CssClass="content" SelectionMode="Multiple"></asp:ListBox>
<asp:CustomValidator ID="CV_SelectionCount" runat="server" ValidateEmptyText="true" ClientValidationFunction="validateSelectionCount" OnServerValidate="validateSelectionCount" ControlToValidate="ListBox1" Display="None" EnableClientScript="true" ErrorMessage="Select 3 items" Style="visibility: hidden" ValidationGroup="VG_SAVE">*</asp:CustomValidator>
So要识趣 2024-11-24 19:23:06

Saurabh 是对的,将 SelectionMode 的 Listbox 属性设置为 Multiple。这将允许您从列表框中进行多项选择。

Saurabh is right, Set you Listbox property of SelectionMode to Multiple. This will allow you to make multiple selections from your listbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文