zoukankan      html  css  js  c++  java
  • ASP.NET DropDownList 控件绑定数据

    今天 遇到 DropDownList 绑定数据的问题, 瞬间让自己搞晕了。

    前台页面 就一个DropDownList :

      <asp:DropDownList ID="dropDownListOne" runat="server">         
            </asp:DropDownList>

     在 aspx.cs绑定数据:

    数据源是键值对的 字符串 或 类似这样类型的Object: 

    String test = "{'100000' : 18 , '200000':36, '300000':54, '400000':72, '5000000':90}";
    

    效果要是这样的:

    <select name="dropDownListOne" id="dropDownListOne">
    	<option value="18">100000</option>
    	<option value="36">200000</option>
    	<option value="54">300000</option>
    	<option value="72">400000</option>
    	<option value="90">5000000</option>
    
    </select>
    

      刚开始 瞬间懵比了, 想不起来怎么做, 后来想到一种方法:

    String test = "{'100000' : 18 , '200000':36, '300000':54, '400000':72, '5000000':90}";
    IDictionary<string, int> list = Newtonsoft.Json.JsonConvert.DeserializeObject<IDictionary<string, int>>(test);
    //// 绑定数据源  
    dropDownListOne.DataSource = list; dropDownListOne.DataTextField = "key"; dropDownListOne.DataValueField = "value"; dropDownListOne.DataBind();

     这样完美解决问题!  不知道 还有没有其他的方法解决问题??

  • 相关阅读:
    第一阶段冲刺8
    第一阶段冲刺7
    第一阶段冲刺6
    第一阶段冲刺5
    第一阶段冲刺4
    第一阶段冲刺3
    冲刺阶段二
    典型用户和用户场景
    团队题目需求分析-NABCD
    第二阶段第七天
  • 原文地址:https://www.cnblogs.com/generalLi/p/6231441.html
Copyright © 2011-2022 走看看