新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實(shí)現(xiàn)ListBox多選并顯示數(shù)據(jù)的方法
本文向大家介紹C# ListBox多選,可能好多人還不了解C# ListBox多選,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)寧海免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
C# ListBox多選并顯示數(shù)據(jù)似乎有些難,但是大家看了筆者實(shí)現(xiàn)此內(nèi)容的代碼已經(jīng)就不會(huì)覺得很難了。
- <%@ 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語句之間的空格,否則出錯(cuò)
- 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;
- }
- }
怎樣,筆者分享的C# ListBox多選并顯示數(shù)據(jù)的代碼希望大家有用!
分享題目:實(shí)現(xiàn)ListBox多選并顯示數(shù)據(jù)的方法
URL標(biāo)題:http://www.dlmjj.cn/article/djpsoie.html


咨詢
建站咨詢
