如何把数据库中的内容绑定到CheckBoxList上?

本文介绍了如何将数据库中的数据绑定到ASP.NET的CheckBoxList控件上,以实现显示用户上次选择的权限项。通过示例展示了利用Login和ProdClass两个表,以及在ProdClass表中增加UserID字段来实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们不但要把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>

 后台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 窗体设计器生成的代码

        
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();
                
        }

                
    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值