C#后台动态生成CheckBoxList

来源:互联网 发布:mac 终端偏好设置 编辑:程序博客网 时间:2024/05/17 23:55

效果如图:

前台页面插入

<asp:CheckBoxList ID="bandList" runat="server" RepeatDirection="Horizontal"(控制是水平排列还是竖直排列)>
</asp:CheckBoxList>

后台代码:

 string str = "select brandID,brandname from xxxx";
        DataSet ds = ConnDB(str);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            string brandId = row[0].ToString();
            string brandName = row[1].ToString();

    /******************************/
            ListItem newItem = new ListItem();
            newItem.Text = brandName;
            newItem.Value = brandId;


            bandList.Items.Add(newItem);
        }

        dbHelper.closeConn();


两种方法获取前台选择的结果:

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
      if (CheckBoxList1.Items[i].Selected)
      Response.Write("你选的是" +CheckBoxList1.Items[i].Value+ CheckBoxList1.Items[i].Text + "<br>");
}

 foreach (ListItem item in SupporFacilities_CheckBoxList.Items)
 {
     if (item.Selected == true)
     SupporFacilities_Value += item.Text + ",";
 }


0 0
原创粉丝点击