【问题标题】:sorted list and drop down list in asp.net using c#使用 c# 在 asp.net 中排序列表和下拉列表
【发布时间】:2011-08-27 18:47:05
【问题描述】:

我有一个返回 sortedList 的方法,我想将它数据源到 Dropdownlist。

我正在使用

DropDownList1.DataSource=stList;
DropDownList1.DataValueField=stList.ContainsValue();
DropDownList1.DataTextField=stList.ContainsKey();
DropDownList1.DataBind();

但它给出了一个错误:没有 containsKey 和 containsValue 的重载方法。 如何在下拉列表中填充这个排序表?

【问题讨论】:

    标签: c# .net asp.net generics data-binding


    【解决方案1】:
        Dim SL As New SortedList(Of String, String)
        SL.Add("A", "1")
        SL.Add("B", "2")
    
        DD1.DataSource = SL
        DD1.DataTextField = "key"
        DD1.DataValueField = "value"
        DD1.DataBind()
    

    【讨论】:

      【解决方案2】:
      DropDownList1.DataSource = stList;
      DropDownList1.DataValueField = "Key";
      DropDownList1.DataTextField = "Value";
      DropDownList1.DataBind();
      

      [编辑]

      添加经过测试的工作代码:

      SortedList<int, string> list = new SortedList<int, string>();
      list.Add(1, "Test1");
      list.Add(2, "Test2");
      
      dropDownList.DataTextField = "Value";
      dropDownList.DataValueField = "Key";
      dropDownList.DataSource = list;
      dropDownList.DataBind();
      

      【讨论】:

      • 谢谢.. 我使用它,但它在下拉列表中显示 System.Collection.SortedList。
      • 它没有错误,但在下拉列表中而不是显示值它显示 System.Collection.SortedList
      • @user1 - 我测试了这段代码。用我使用的复制/粘贴代码更新了我的答案,并验证它是否正常工作。
      猜你喜欢
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多