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

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

posted @ 2016-12-29 00:43  jason_LSZ  阅读(334)  评论(0编辑  收藏  举报