有时候我们不但要把CheckBoxList的数据写入到数据库,还要把数据库的内容与CheckBoxList对应,让他显示上次我们选中的项,我是这么实现的:
比方说我们要给用户赋予不同的权限,那么我们数据库有Login,ProdClass两张表,那么我们在ProdClass表中插入一个字段UserID,下面是程序 :
UserClass.aspx页面:
<%@ Page language="c#" Codebehind="UserClass.aspx.cs" AutoEventWireup="false" Inherits="LabWeb.adminstra.UserClass" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>UserClass</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:radiobuttonlist id="RadioButtonList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
AutoPostBack="True"></asp:radiobuttonlist>
<hr color="red">
</FONT>
<asp:checkboxlist id="CheckBoxList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"></asp:checkboxlist>
<hr color="red">
<asp:button id="Button1" runat="server" Text="设置分类" Width="176px"></asp:button></form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>UserClass</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:radiobuttonlist id="RadioButtonList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
AutoPostBack="True"></asp:radiobuttonlist>
<hr color="red">
</FONT>
<asp:checkboxlist id="CheckBoxList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"></asp:checkboxlist>
<hr color="red">
<asp:button id="Button1" runat="server" Text="设置分类" Width="176px"></asp:button></form>
</body>
</HTML>
后台UserClass.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace LabWeb.adminstra
...{
/**//// <summary>
/// UserClass 的摘要说明。
/// </summary>
public class UserClass : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
private void Page_Load(object sender, System.EventArgs e)
...{
if(!this.IsPostBack)
...{
//绑定用户
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd=new SqlCommand("select * from Login",con);
SqlDataReader sdr =cmd.ExecuteReader();
this.RadioButtonList1.DataSource=sdr;
this.RadioButtonList1.DataTextField="UserName";
this.RadioButtonList1.DataValueField="UserID";
this.RadioButtonList1.DataBind();
sdr.Close();
//绑定类别,LabSedtype为你的类别表
cmd.CommandText="select * from LabSedtype";
SqlDataReader sdr1=cmd.ExecuteReader();
this.CheckBoxList1.DataSource=sdr1;
this.CheckBoxList1.DataTextField="SecondTypeName";
this.CheckBoxList1.DataValueField="SecondTypeID";
this.CheckBoxList1.DataBind();
sdr1.Close();
con.Close();
}
// 在此处放置用户代码以初始化页面
}

Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
...{
//判断是否选中
string UserID=this.RadioButtonList1.SelectedValue;
if(UserID=="")
...{
Response.Write("<script>alert('请选择要设置权限的管理员!')</script>");
}
else
...{
string SelectID="0";
foreach(ListItem chk in this.CheckBoxList1.Items)
...{
if(chk.Selected)
...{
SelectID=chk.Value;
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd =new SqlCommand("Update LabSedType set UserID='"+UserID+"' where SecondTypeID ='"+SelectID+"' ",con);
cmd.ExecuteNonQuery();
con.Close();
}
else
...{
string NoSelected=chk.Value;
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd =new SqlCommand("Update LabSedType set UserID='' where SecondTypeID ='"+NoSelected+"'and UserID='"+UserID+"' ",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Write("<font color=Red size=14px>您已经成功设置类别编号!!</font>");
}

}
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
...{
//当选择了用户的时候,自动显示出他的管理类别
foreach(ListItem chk in this.CheckBoxList1.Items)
...{
chk.Selected=false;
}
string SecondTypeID="";
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd =new SqlCommand("select SecondTypeID from LabSedType where UserID='"+this.RadioButtonList1.SelectedValue+"'",con);
SqlDataReader sdr =cmd.ExecuteReader();
while(sdr.Read())
...{
SecondTypeID=sdr.GetInt32(0).ToString();
foreach(ListItem chk in this.CheckBoxList1.Items)
...{
if(chk.Value==SecondTypeID)
...{
chk.Selected=true;
}
}
}
sdr.Close();
con.Close();
}
}
}
本文介绍了如何将数据库中的数据绑定到ASP.NET的CheckBoxList控件上,以实现显示用户上次选择的权限项。通过示例展示了利用Login和ProdClass两个表,以及在ProdClass表中增加UserID字段来实现这一功能。
1430





