简单实现C#ListBox获取多选项的值

关于C#  ListBox有好多方面要讲,今天笔者为大家准备的是如何实现C# ListBox获取多选项的值。

 
 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             BindListBox();  
  6.         }  
  7.     }  
  8.     #region 绑定类别名称到ListBox  
  9.     ///   
  10.     /// 绑定类别名称到ListBox  
  11.     /// 
  12.  
  13.     void BindListBox()  
  14.     {  
  15.         string sql = "SELECT * FROM ClassTable WHERE PlateID=2 OR PlateID=4";  
  16.         DataTable myClassTable = new ClassTable_bll().WhereToClassTable(sql);  
  17.         ListBox1.DataTextField = "MerchClassName";  
  18.         ListBox1.DataValueField = "ClassID";  
  19.         ListBox1.DataSource = myClassTable.DefaultView;  
  20.         ListBox1.DataBind();  
  21.     }  
  22.     #endregion  
  23.     protected void Button1_Click(object sender, EventArgs e)  
  24.     {  
  25.         string str = "";  
  26.         for (int i = 0; i < ListBox1.Items.Count; i++)  
  27.         {  
  28.             if (ListBox1.Items[i].Selected == true)  
  29.             {  
  30.                 str = str + ListBox1.Items[i].Text+",";                  
  31.             }  
  32.         }  
  33.         this.Response.Write(" alert('"  + str + "');");  
  34.     }  

以上就是关于C#  ListBox获取多选项的值的方法介绍。


本文标题:简单实现C#ListBox获取多选项的值
URL地址:http://gydahua.com/article/djeecec.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流