---
title: "ASP.NET2.0中CheckBoxList控件与数据库绑定（学习）"
author: "jandroid"
date: "2007-05-07T17:26:00Z"
tags: []
slug: "asp-net2-0-checkboxlist"
draft: false
lastmod: "2011-11-14T09:35:42Z"
id: 1257407
url: "https://jandroid.iteye.com/blog/1257407"
---

新建一个Webform页面，命名为like.aspx（用来显示爱好列表），拉进一个CheckBoxList控件，在属性设置为分为两列显示（ReapeatColumn= 2）。

添加一个类，命名为DB.cs（用来进行数据库链接），代码如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Configuration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.Security;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.WebControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.WebControls.WebParts;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.HtmlControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data.SqlClient;//自己加的命名空间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///DB的摘要说明  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassDB  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicDB()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODO:在此处添加构造函数逻辑  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticSqlConnectioncreateConnection()//自己创建  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建一个静态的数据库链接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.Data.SqlClient.SqlConnectioncon=newSqlConnection("server=.;database=login;uid=sa;pwd=\*\*\*\*\*\*\*\*\*;");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returncon;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

使用SQL创建数据库和创建表（数据库名：login 表名：personlike）,代码如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)uselogin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createtablepersonlike  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)idintprimarykey,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)likecontentvarchar(100)notnull  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertintopersonlikevalues(1,'体育')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertintopersonlikevalues(2,'旅游')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertintopersonlikevalues(3,'足球')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertintopersonlikevalues(4,'篮球')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)insertintopersonlikevalues(5,'游泳')

双击CheckBoxList控件，响应控件事件，代码如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Configuration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Collections;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.Security;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.WebControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.WebControls.WebParts;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.HtmlControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data.SqlClient;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicpartialclasslike:System.Web.UI.Page  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedvoidPage\_Load(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{//自己添加的代码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SqlConnectioncon=DB.createConnection();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)con.Open();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SqlCommandcmd=newSqlCommand("select\*frompersonlike",con);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SqlDataReadersdr=cmd.ExecuteReader();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.CheckBoxList1.DataTextField="likecontent";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.CheckBoxList1.DataValueField="id";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.CheckBoxList1.DataSource=sdr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.CheckBoxList1.DataBind();//与控件进行绑定  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sdr.Close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)con.Close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedvoidCheckBoxList1\_SelectedIndexChanged(objectsender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}