实现ListBox多选并显示数据的方法

开发 后端
C# ListBox实现多选并显示数据似乎有些难,但是大家看了笔者实现此内容的代码已经就不会觉得很难了。笔者分享的C# ListBox多选并显示数据的代码希望大家有用!

本文向大家介绍C# ListBox多选,可能好多人还不了解C# ListBox多选,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

C#  ListBox多选并显示数据似乎有些难,但是大家看了笔者实现此内容的代码已经就不会觉得很难了。

  1. <%@ Page Language="C#" AutoEventWireup="true" Debug="true"%>  
  2. <%@import namespace="System.Data"%>  
  3. <%@import namespace="System.Data.SqlClient"%>  
  4.  
  5.  
  6.  
  7. "server">  
  8. "ckbEmployees" runat="server" RepeatLayout="table" RepeatDirection="vertical" RepeatColumns="3" CellPadding="9" CellSpacing="18" TextAlign="right" OnSelectedIndexChanged="subListChange" AutoPostBack="true" />  


  9.  
  10. "dgEmployee" runat="server" />  
  11.  
  12.  
  13.  
  14. "c#" runat="server">  
  15. private void Page_load(object sender,System.EventArgs e)  
  16. {  
  17. if(!IsPostBack)  
  18. {  
  19.    string strConnection ="server=.;uid=sa;pwd=sunix!;database=northwind";  
  20.    string strSQLforCheckBoxes = "select LastName ,EmployeeID from employees order by lastname";  
  21.    SqlConnection objConnection = new SqlConnection(strConnection);  
  22.    SqlCommand objCommand = new SqlCommand(strSQLforCheckBoxes,objConnection);  
  23.    objConnection.Open();  
  24.    ckbEmployees.DataSource = objCommand.ExecuteReader();  
  25.    ckbEmployees.DataTextField = "LastName";  
  26.    ckbEmployees.DataValueField = "EmployeeID";  
  27.    ckbEmployees.DataBind();  
  28.    objConnection.Close();  
  29. }  
  30. }  
  31. private void subListChange(object s,System.EventArgs e)  
  32. {  
  33. Response.Write("subListchange triggered
    "
    );  
  34. string strWhereClause="";  
  35. foreach (ListItem liThisOne in ckbEmployees.Items)  
  36. {  
  37.    if(liThisOne.Selected)  
  38.    {  
  39.     strWhereClause += "EmployeeID = " + liThisOne.Value + " OR ";  
  40.    }  
  41. }  
  42. Response.Write("strWhereClause=
    "
    +strWhereClause+"
    "
    );  
  43. if(strWhereClause.Length>0)  
  44. {  
  45.    dgEmployee.Visible = true;  
  46.    string str = strWhereClause.Substring(0,strWhereClause.Length - 3);  
  47.    strWhereClause = " where " + str;  
  48.    string strConnection = "server=.;uid=sa;pwd=sunix!;database=northwind";  
  49.    string strSQLforGrid = "select TitleOfCourtesy,firstName,lastName,country,region,city,notes from employees " + strWhereClause;  
  50.      //Response.Write(strSQLforGrid); sql语句之间的空格,否则出错  
  51.    SqlConnection objConnection = new SqlConnection(strConnection);  
  52.    SqlCommand objCommand = new SqlCommand(strSQLforGrid,objConnection);  
  53.    Response.Write("strSQLforGrid=
    "
    +strSQLforGrid+"
    "
    );  
  54.    objConnection.Open();  
  55.    dgEmployee.DataSource = objCommand.ExecuteReader();  
  56.    dgEmployee.DataBind();  
  57.    objConnection.Close();  
  58. }  
  59. else 
  60. {  
  61.    dgEmployee.Visible = false;  
  62. }  
  63. }  
  64.     

怎样,笔者分享的C#  ListBox多选并显示数据的代码希望大家有用!

【编辑推荐】

  1. C#委托基础:谈委托和接口
  2. 简单实现C# CheckBox单选的相关功能
  3. C# ServiceController类剖析
  4. C# HttpWebRequest提交数据方式浅析
  5. C#计算素数序列浅谈
责任编辑:阡陌 来源: 51CTO.com
相关推荐

2009-09-08 16:01:58

C# ListBox

2009-09-08 15:12:07

C# ListBox

2010-01-21 17:58:40

VB.NET List

2009-09-08 14:54:40

C# listBox控

2009-09-08 16:22:27

c# listBox

2015-01-06 16:41:32

Swiftenum多选

2009-12-28 11:14:29

WPF显示文本

2011-07-14 14:36:29

Dbgrid多数据库

2009-09-08 16:34:49

C# Listbox

2009-09-10 12:00:09

C# listbox

2012-03-01 19:44:18

Android

2009-09-08 15:50:44

c# listbox

2009-12-24 14:08:25

WPF数据模板

2011-07-27 16:31:53

MySQL数据库utf8gbk

2009-09-08 14:43:41

C# listbox

2011-07-22 09:43:53

iPhone XML POST

2010-11-23 13:51:55

MySQL数据表

2010-08-18 09:03:46

jQueryJSONTrimpath

2009-09-17 08:47:00

Linq插入数据

2010-01-27 17:53:18

Android显示网络
点赞
收藏

51CTO技术栈公众号