0

我目前有两个 ListBox(ListBox1 和 ListBox2),ListBox1 中有 12 个静态值(value1、value2、value3 等),允许用户使用添加和删除按钮在它们之间传输值。我也有一个下拉框。当在该下拉框中进行特定选择时,如何在 ListBox2 上强制执行最大值?换句话说,如果我只想在下拉框中选择一个值时允许将 Listbox1 中的最多一个条目移动到 Listbox2。

 protected void MoveRight(object sender, EventArgs e)
{
    while (ListBox1.Items.Count > 0 && ListBox1.SelectedItem != null)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
    }
}
protected void MoveLeft(object sender, EventArgs e)
{
    while (ListBox2.Items.Count > 0 && ListBox2.SelectedItem != null)
    {
        ListItem selectedItem = ListBox2.SelectedItem;
        selectedItem.Selected = false;
        ListBox1.Items.Add(selectedItem);
        ListBox2.Items.Remove(selectedItem);
    }
}
private void BindData()
{
    ListBox1.Items.Add(new ListItem("01", "01"));
    ListBox1.Items.Add(new ListItem("02", "02"));
    ListBox1.Items.Add(new ListItem("03", "03"));
    ListBox1.Items.Add(new ListItem("04", "04"));
    ListBox1.Items.Add(new ListItem("05", "05"));
    ListBox1.Items.Add(new ListItem("06", "06"));
    ListBox1.Items.Add(new ListItem("07", "07"));
    ListBox1.Items.Add(new ListItem("08", "08"));
    ListBox1.Items.Add(new ListItem("09", "09"));
    ListBox1.Items.Add(new ListItem("10", "10"));
    ListBox1.Items.Add(new ListItem("11", "11"));
    ListBox1.Items.Add(new ListItem("12", "12"));

}
4

3 回答 3

0

您希望如何在代码隐藏中处理它?如果您想在下拉列表设置为 1 时复制最多一个选定项目,您可以执行以下操作:

protected void MoveRight(object sender, EventArgs e)
{
    int max = Convert.ToInt32(DropDownList1.SelectedValue);
    for(int i=0;i<max && ListBox1.Items.Count > 0 && ListBox1.SelectedItem != null; i++)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
   }

}

或者,如果您希望将ListBox1下拉列表设置为 1 时选择的 2 个项目的情况作为验证错误,您可以为 a 的ServerValidate事件编写一个处理程序CustomValidator

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs e)
{
    // There's probably a simpler way to get a count of items selected
    e.IsValid = ListBox1.Items.Count(li=> li.Selected) <= Convert.ToInt32(DropDownList1.SelectedValue);
}

或者,如果您希望在客户端发生这种情况,则必须编写一些 javascript。

于 2013-03-26T02:08:55.213 回答
0

您可以尝试使用此代码

int YourMax = 10;
if(ListBox1.Items.Count < Yourmax)
{
   //Add item
       }

链接:http: //msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection_members (v=vs.80) .aspx

注意:您没有满足此需求的财产

这里的所有属性:http: //msdn.microsoft.com/fr-fr/library/aeb9t2b5 (v=vs.80).aspx

于 2013-03-25T16:40:30.850 回答
0

使用 for 循环迭代您的最大值和列表中的项目数中的较小者。

protected void MoveRight(object sender, EventArgs e)
{
    int max = 1;
    int iterations = ListBox1.Items.Count < max ? ListBox1.Items.Count : max
    for(int i = 0; i < iterations; i++)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        if(selectedItem == null)
            break;

        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
    }
}

现在您可以max进入类定义并根据需要对其进行操作。

于 2013-03-25T16:40:50.420 回答