关于C# ListBox有好多方面要讲,今天笔者为大家准备的是如何实现C# ListBox获取多选项的值。
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindListBox();
- }
- }
- #region 绑定类别名称到ListBox
- ///
- /// 绑定类别名称到ListBox
- ///
- void BindListBox()
- {
- string sql = "SELECT * FROM ClassTable WHERE PlateID=2 OR PlateID=4";
- DataTable myClassTable = new ClassTable_bll().WhereToClassTable(sql);
- ListBox1.DataTextField = "MerchClassName";
- ListBox1.DataValueField = "ClassID";
- ListBox1.DataSource = myClassTable.DefaultView;
- ListBox1.DataBind();
- }
- #endregion
- protected void Button1_Click(object sender, EventArgs e)
- {
- string str = "";
- for (int i = 0; i < ListBox1.Items.Count; i++)
- {
- if (ListBox1.Items[i].Selected == true)
- {
- str = str + ListBox1.Items[i].Text+",";
- }
- }
- this.Response.Write("");
- }
以上就是关于C# ListBox获取多选项的值的方法介绍。
【编辑推荐】