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

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

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

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

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


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

怎样,笔者分享的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多选

2024-07-31 20:39:49

前端文本JavaScrip

2009-09-08 16:34:49

C# Listbox

2009-09-10 12:00:09

C# listbox

2011-07-14 14:36:29

Dbgrid多数据库

2009-09-08 15:50:44

c# listbox

2009-12-28 11:14:29

WPF显示文本

2012-03-01 19:44:18

Android

2009-09-08 14:43:41

C# listbox

2009-12-24 14:08:25

WPF数据模板

2011-07-22 09:43:53

iPhone XML POST

2011-07-27 16:31:53

MySQL数据库utf8gbk

2010-11-23 13:51:55

MySQL数据表

2009-09-17 08:47:00

Linq插入数据

2010-01-27 17:53:18

Android显示网络
点赞
收藏

51CTO技术栈公众号