【问题标题】:Listbox custom property列表框自定义属性
【发布时间】:2015-04-13 11:52:06
【问题描述】:

我正在构建一个 Web 应用程序,它需要在 ListBox 样式控件中显示 100 个(可能超过 1000 个)项目。但是,我需要为每个项目保存的不仅仅是 textvalue 数据。 我尝试在 itemtemplate 中使用带有表格的中继器,它可以工作并以我想要的方式显示,但性能很差。

数据来自 SQL 数据源。

有没有办法自定义 ListBox 控件以允许每个列表项有多个值?

【问题讨论】:

  • 如果你想要那些在客户端的自定义属性,即只有 javascript,那么你可以将它们添加到 listitem 作为属性,我可以帮助你,让我知道这是否是你的要求?但是如果你想在服务器端更改那些自定义属性和访问,那可能是不可行的。
  • 不幸的是它需要在服务器端。所有信息都来自 SQL,并且还有一个 postback 来更新 SQL 表,所以我认为 js 不会削减它
  • 好的,想法是将这些自定义值设置为listitem,如果通过 JS 对这些值进行任何更改,您可以将相同的内容存储到页面回发的隐藏字段中从隐藏字段中获取这些值并重新保存到 DB>

标签: javascript c# jquery asp.net webforms


【解决方案1】:

让我一步一步解释它

首先,您需要一个启用多选的列表框,您必须像这样使用列表框

<asp:ListBox ID="ListBox1" runat="server" Height="149px" SelectionMode="Multiple" Width="113px">

然后你想对 ListboxItem 执行操作,因为你必须执行类似的操作

 private void UpClick()
{
    // only if the first item isn't the current one
    if(listBox1.ListIndex > 0)
    {
        // add a duplicate item up in the listbox
        listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
        // make it the current item
        listBox1.ListIndex = (listBox1.ListIndex - 2);
        // delete the old occurrence of this item
        listBox1.RemoveItem(listBox1.ListIndex + 2);
    }
}



 private void DownClick()
{
   // only if the last item isn't the current one
   if((listBox1.ListIndex != -1) && (listBox1.ListIndex < listBox1.ListCount - 1))
   {
      // add a duplicate item down in the listbox
      listBox1.AddItem(listBox1.Text, listBox1.ListIndex + 2);
      // make it the current item
      listBox1.ListIndex = listBox1.ListIndex + 2;
      // delete the old occurrence of this item
      listBox1.RemoveItem(listBox1.ListIndex - 2);
   }
}

对于全选,您可以清除 Listbox2 并将 Listbox1 到 Listbox2 中的所有项目填充。

【讨论】:

  • Ty,我确实提到了将物品从一个盒子移动到另一个盒子不是问题......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2012-12-09
相关资源
最近更新 更多