ASP.NET 数据绑定到列表控件

<div>
    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
</div>
protected void Page_Load(object sender, EventArgs e)
{
    List<string> fruit = new List<string>();
    fruit.Add("Kiwi");
    fruit.Add("Pear");
    fruit.Add("Mango");
    fruit.Add("Blueberry");
    fruit.Add("Apricot");
    fruit.Add("Banana");
    fruit.Add("Peach");
    fruit.Add("Plum");

    ListBox1.DataSource = fruit;
    DropDownList1.DataSource = fruit;
    CheckBoxList1.DataSource = fruit;
    RadioButtonList1.DataSource = fruit;

    this.DataBind();
}

 

进阶:数据绑定到字典集合

protected void Page_Load(Object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        // Use integers to index each item. Each item is a string.
        Dictionary<int, string> fruit = new Dictionary<int, string>();
  
        fruit.Add(1, "Kiwi");
        fruit.Add(2, "Pear");
        fruit.Add(3, "Mango");
        fruit.Add(4, "Blueberry");
        fruit.Add(5, "Apricot");
        fruit.Add(6, "Banana");
        fruit.Add(7, "Peach");
        fruit.Add(8, "Plum");
  
        // Define the binding for the list controls.
        MyListBox.DataSource = fruit;
      
/*
MyListBox.DataTextField = "Value";
MyListBox.DataValueField = "Key";
*/

// Choose what you want to display in the list. MyListBox.DataTextField = "Value"; // Activate the binding. this.DataBind(); } }

html code:

<select name="MyListBox" id="MyListBox" >
    <option value="1">Kiwi</option>
    <option value="2">Pear</option>
    <option value="3">Mango</option>
    <option value="4">Blueberry</option>
    <option value="5">Apricot</option>
    <option value="6">Banana</option>
    <option value="7">Peach</option>
    <option value="8">Plum</option>
</select>

 

转载于:https://www.cnblogs.com/wanghaibin/p/3872149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值