C# Web控件与数据感应之 CheckBoxList 类

目录

关于数据感应

CheckBoxList 类

范例运行环境

数据源表设计

角色字典表

用户角色表

AutoValueDBList 方法

原理

设计

实现

调用示例

初始化数据

启动查询模式

使用保存模式

小结


关于数据感应

数据感应也即数据捆绑,是一种动态的,Web控件与数据源之间的交互,本文将继续介绍与数据库提取数据并捆绑到 CheckBoxList 类控件为例,另外同时将控件的值保存回数据库的通用方法。

CheckBoxList 类

System.Web.UI.WebControls.CheckBoxList 类是提供了一组可复选的选项集合,每个选项以true或false 表示其选中状态。其使用方法基于 ListControl 类。

更多 CheckBoxList 类的介绍请参照如下链接:

https://learn.microsoft.com/zh-cn/previous-versions/visualstudio/design-tools/expression-studio-2/cc294907(v=expression.10)

范例运行环境

操作系统: Windows Server 2019 DataCenter

.net版本: .netFramework4.7.1 或以上

开发工具:VS2019  C#

数据提取:在这里我们以MS SQL Server 2016为例

数据源表设计

我们假设要为用户添加角色权限,则需要涉及两个表:

角色字典表

表(sys_chars)用于列出可用的角色,其结构如下:

序号字段名类型说明备注
1ciduniqueidentifier唯一ID用于后续方法使用
2charnamenvarchar(30)角色名称

其数据示例如下:

用户角色表

表(sys_UserChars)用于存储用户的可用角色(用户ID+角色ID 唯一),其结构如下:

序号字段名类型说明备注
1user_ciduniqueidentifier用户ID用户的ID值
2char_ciduniqueidentifier角色名称用记所属的角色ID值

其示例数据如下:

AutoValueDBList 方法

原理

我们需要提取 sys_chars (角色字典表) 数据绑定到 CheckBoxList 控件上,用于显示可用的角色名称。绑定后通过 AutoValueDBList 方法的查询模式,从 sys_UserChars (用户角色表)提取数据并与 CheckBoxList 上的项进行比对,存在的则选中。同理,使用 AutoValueDBList 方法的保存模式,则将用户在 CheckBoxList 上的选项逐一保存到 sys_UserChars (用户角色表)里。

设计

AutoValueDBList 方法主要分查询模式和保存模式,在保存模式的情况下返回成功影响的行数,其参数说明如下表:

序号参数名类型说明
1strConnstring对应数据库的连接字符串
2_objectListControl要感应的 ListControl 类控件,这里泛指 CheckBoxList
3AutoTypestring

两种值可选择,“query” 为查询模式,“save” 为保存模式

4keyFieldTypestring

连接的目标表的关键字字段类型,如 uniqueidentifier,比如sys_UserChars 中的 user_cid 字段类型

5linkKeyValuestring连接的目标表的关键字段的值,比如sys_UserChars 中的 user_cid 字段的值
6Tablenamestring要连接的目标表比如 sys_UserChars
7KeyFieldstring连接的目标表的关键字字段名,比如sys_UserChars 中的字段 “user_cid” 
8KeyField2string连接的目标表的第二关键字字段名,比如sys_UserChars 中的字段 “char_cid” 
9CidFieldNamestring指定连接目标表的唯一标识字段名,这里仅允许使用 uniqueidentifier 的类型字段,如无则默认不参于 insert 操作,设置则表示其值为 newid()

实现

AutoValueDBList 方法完整代码如下:

public int AutoValueDBList(string strConn,ListControl _object,string AutoType,string keyFieldType,string linkKeyValue,string Tablename,string KeyField,string KeyField2,string CidFieldName)
{
			int rv=-1;
			SqlDbType type=GetSqlDbType(keyFieldType);

			SqlConnection Conn = new SqlConnection(strConn );
			SqlCommand Cmd = new SqlCommand();
			Cmd.Connection = Conn;
			if(AutoType=="save")
			{
				Cmd.CommandText = " delete from "+Tablename+" where "+KeyField+"=@"+KeyField;
				SqlParameter   para1=new SqlParameter("@"+KeyField,type);
				para1.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(linkKeyValue)):(object)linkKeyValue);
				Cmd.Parameters.Add(para1);
				try
				{
					Conn.Open();
					Cmd.ExecuteNonQuery();
					Cmd.CommandText = " insert into "+Tablename+"("+KeyField+","+KeyField2+") values(@"+KeyField+",@"+KeyField2+")";
					if(CidFieldName!="")
					{
						Cmd.CommandText = " insert into "+Tablename+"("+CidFieldName+","+KeyField+","+KeyField2+") values(newid(),@"+KeyField+",@"+KeyField2+")";
					}
					SqlParameter   para2=new SqlParameter("@"+KeyField2,type);
					Cmd.Parameters.Add(para2);
					int success=0;
					for(int i=0;i<_object.Items.Count;i++)
					{
						string _value=_object.Items[i].Value;
						if(!_object.Items[i].Selected){continue;}
						para2.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(_value)):(object)_value);
						int si=Cmd.ExecuteNonQuery();
						success+=si;
					}
					return success;

				}
				catch (Exception ex)
				{
                    return rv;
				}
				finally
				{
					Conn.Close();
					Conn.Dispose();
				}
			}
			if(AutoType=="query")
			{
				SqlDataReader myDr;
				Cmd.CommandText = " select "+KeyField2+" from "+Tablename+" where "+KeyField+"=@"+KeyField;
				SqlParameter   para1=new SqlParameter("@"+KeyField,type);
				para1.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(linkKeyValue)):(object)linkKeyValue);
				Cmd.Parameters.Add(para1);
				try
				{
					Conn.Open();
					myDr=Cmd.ExecuteReader();
					for(int i=0;i<_object.Items.Count;i++)
					{
						_object.Items[i].Selected=false;
					}
					rv=0;
					while(myDr.Read())
							   {
						rv++;
								   string _dbkey=myDr[KeyField2].ToString();
								   for(int i=0;i<_object.Items.Count;i++)
								   {
									   string _value=_object.Items[i].Value;
									   if(_value==_dbkey){
										   _object.Items[i].Selected=true;
										}
								   }
							   }
				}
				catch (Exception ex)
				{
                    return rv;
				}
				finally
				{
					Conn.Close();
					Conn.Dispose();
				}
			}
			return rv;

}




public SqlDbType GetSqlDbType(String TypeInfo) 
{
			TypeInfo=TypeInfo.ToLower();
			//Byte
			if((TypeInfo=="varchar")||(TypeInfo=="system.string"))
			{
				return SqlDbType.VarChar;
			}
			if((TypeInfo=="bigint")||(TypeInfo=="system.int64"))
			{
				return SqlDbType.BigInt;
			}
			if((TypeInfo=="binary")||(TypeInfo=="system.byte[]"))
			{
				return SqlDbType.Binary;
			}
			if((TypeInfo=="bit")||(TypeInfo=="system.boolean"))
			{
				return SqlDbType.Bit;
			}
			if((TypeInfo=="char")||(TypeInfo=="system.char"))
			{
				return SqlDbType.Char;
			}
			if((TypeInfo=="datetime")||(TypeInfo=="system.datetime"))
			{
				return SqlDbType.DateTime;
			}
			if((TypeInfo=="decimal")||(TypeInfo=="system.decimal"))
			{
				return SqlDbType.Decimal;
			}
			if((TypeInfo=="float")||(TypeInfo=="system.double"))
			{
				return SqlDbType.Float;
			}
			if(TypeInfo=="image")
			{
				return SqlDbType.Image;
			}
			if((TypeInfo=="int")||(TypeInfo=="system.int32"))
			{
				return SqlDbType.Int;
			}
			if((TypeInfo=="money")||(TypeInfo=="system.decimal"))
			{
				return SqlDbType.Money;
			}
			if(TypeInfo=="nchar")
			{
				return SqlDbType.NChar;
			}
			if(TypeInfo=="ntext")
			{
				return SqlDbType.NText;
			}
			if(TypeInfo=="nvarchar")
			{
				return SqlDbType.NVarChar;
			}
			if((TypeInfo=="real")||(TypeInfo=="system.single"))
			{
				return SqlDbType.Real;
			}
			if(TypeInfo=="smalldatetime")
			{
				return SqlDbType.SmallDateTime;
			}
			if((TypeInfo=="smallint")||(TypeInfo=="system.int16"))
			{
				return SqlDbType.SmallInt;
			}
			if(TypeInfo=="smallmoney")
			{
				return SqlDbType.SmallMoney;
			}
			if(TypeInfo=="text")
			{
				return SqlDbType.Text;
			}
			if((TypeInfo=="timestamp")||(TypeInfo=="system.timespan"))
			{
				return SqlDbType.Timestamp;
			}
			if((TypeInfo=="tinyint")||(TypeInfo=="system.byte"))
			{
				return SqlDbType.TinyInt;
			}
			if((TypeInfo=="uniqueidentifier")||(TypeInfo=="system.guid"))
			{
				return SqlDbType.UniqueIdentifier;
			}
			if(TypeInfo=="varbinary")
			{
				return SqlDbType.VarBinary;
			}
			if(TypeInfo=="variant")
			{
				return SqlDbType.Variant;
			}
			return SqlDbType.VarChar;
}// end GetSqlDbType function 	

调用示例

初始化数据

假设前端 UI 有 ID  为 CBL 的 CheckBoxList 控件,则调用的示例代码如下: 

simpleDataListEx("sqlserver","数据库连接串","select cid, charname from sys_chars", null, "cid", "charname", CBL, false, "", "","");

有关 simpeDataListEx 的使用方法请阅读我的文章《C# Web控件与数据感应之 Control 类》。 

启动查询模式

初始化完成后,启动 AutoValueDBList 方法的查询模式,进行比对操作。示例代码如下:

string user_cid_value="1F044A84-9154-466B-9B9F-894282625729";
AutoValueDBList("你的数据库连接串",CBL, "query", "uniqueidentifier", user_cid_value, "sys_userchars", "user_cid", "char_cid");

使用保存模式

当用户重新进行复选操作时,可以将结果提交给数据库进行保存。示例代码如下:

string user_cid_value="1F044A84-9154-466B-9B9F-894282625729";
AutoValueDBList("你的数据库连接串",CBL,"save","uniqueidentifier",user_cid_value,"sys_userchars","user_cid","char_cid","cid");

小结

范例中使用的MS SQL SERVER 数据库,我的下载资源还提供了 Oracle 9i及达梦数据库的驱动链接库,请下载我的资源:

https://download.csdn.net/download/michaelline/89235824

我们可以根据需要改造方法,另外 AutoValueDBList 方法基于 ListControl 类,我们可以根据实际的需要进行使用和改造。感谢您的阅读,希望本文能够对您有所帮助。

  • 50
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初九之潜龙勿用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值