本文向大家介绍C# ListBox多选,可能好多人还不了解C# ListBox多选,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
专注于为中小企业提供成都网站制作、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业宁海免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
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;
- }
- }
怎样,笔者分享的C# ListBox多选并显示数据的代码希望大家有用!
网页题目:实现ListBox多选并显示数据的方法
网址分享:http://www.shufengxianlan.com/qtweb/news44/60344.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联