当前位置:   article > 正文

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

c# 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 方法完整代码如下:

  1. public int AutoValueDBList(string strConn,ListControl _object,string AutoType,string keyFieldType,string linkKeyValue,string Tablename,string KeyField,string KeyField2,string CidFieldName)
  2. {
  3. int rv=-1;
  4. SqlDbType type=GetSqlDbType(keyFieldType);
  5. SqlConnection Conn = new SqlConnection(strConn );
  6. SqlCommand Cmd = new SqlCommand();
  7. Cmd.Connection = Conn;
  8. if(AutoType=="save")
  9. {
  10. Cmd.CommandText = " delete from "+Tablename+" where "+KeyField+"=@"+KeyField;
  11. SqlParameter para1=new SqlParameter("@"+KeyField,type);
  12. para1.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(linkKeyValue)):(object)linkKeyValue);
  13. Cmd.Parameters.Add(para1);
  14. try
  15. {
  16. Conn.Open();
  17. Cmd.ExecuteNonQuery();
  18. Cmd.CommandText = " insert into "+Tablename+"("+KeyField+","+KeyField2+") values(@"+KeyField+",@"+KeyField2+")";
  19. if(CidFieldName!="")
  20. {
  21. Cmd.CommandText = " insert into "+Tablename+"("+CidFieldName+","+KeyField+","+KeyField2+") values(newid(),@"+KeyField+",@"+KeyField2+")";
  22. }
  23. SqlParameter para2=new SqlParameter("@"+KeyField2,type);
  24. Cmd.Parameters.Add(para2);
  25. int success=0;
  26. for(int i=0;i<_object.Items.Count;i++)
  27. {
  28. string _value=_object.Items[i].Value;
  29. if(!_object.Items[i].Selected){continue;}
  30. para2.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(_value)):(object)_value);
  31. int si=Cmd.ExecuteNonQuery();
  32. success+=si;
  33. }
  34. return success;
  35. }
  36. catch (Exception ex)
  37. {
  38. return rv;
  39. }
  40. finally
  41. {
  42. Conn.Close();
  43. Conn.Dispose();
  44. }
  45. }
  46. if(AutoType=="query")
  47. {
  48. SqlDataReader myDr;
  49. Cmd.CommandText = " select "+KeyField2+" from "+Tablename+" where "+KeyField+"=@"+KeyField;
  50. SqlParameter para1=new SqlParameter("@"+KeyField,type);
  51. para1.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(linkKeyValue)):(object)linkKeyValue);
  52. Cmd.Parameters.Add(para1);
  53. try
  54. {
  55. Conn.Open();
  56. myDr=Cmd.ExecuteReader();
  57. for(int i=0;i<_object.Items.Count;i++)
  58. {
  59. _object.Items[i].Selected=false;
  60. }
  61. rv=0;
  62. while(myDr.Read())
  63. {
  64. rv++;
  65. string _dbkey=myDr[KeyField2].ToString();
  66. for(int i=0;i<_object.Items.Count;i++)
  67. {
  68. string _value=_object.Items[i].Value;
  69. if(_value==_dbkey){
  70. _object.Items[i].Selected=true;
  71. }
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. return rv;
  78. }
  79. finally
  80. {
  81. Conn.Close();
  82. Conn.Dispose();
  83. }
  84. }
  85. return rv;
  86. }
  87. public SqlDbType GetSqlDbType(String TypeInfo)
  88. {
  89. TypeInfo=TypeInfo.ToLower();
  90. //Byte
  91. if((TypeInfo=="varchar")||(TypeInfo=="system.string"))
  92. {
  93. return SqlDbType.VarChar;
  94. }
  95. if((TypeInfo=="bigint")||(TypeInfo=="system.int64"))
  96. {
  97. return SqlDbType.BigInt;
  98. }
  99. if((TypeInfo=="binary")||(TypeInfo=="system.byte[]"))
  100. {
  101. return SqlDbType.Binary;
  102. }
  103. if((TypeInfo=="bit")||(TypeInfo=="system.boolean"))
  104. {
  105. return SqlDbType.Bit;
  106. }
  107. if((TypeInfo=="char")||(TypeInfo=="system.char"))
  108. {
  109. return SqlDbType.Char;
  110. }
  111. if((TypeInfo=="datetime")||(TypeInfo=="system.datetime"))
  112. {
  113. return SqlDbType.DateTime;
  114. }
  115. if((TypeInfo=="decimal")||(TypeInfo=="system.decimal"))
  116. {
  117. return SqlDbType.Decimal;
  118. }
  119. if((TypeInfo=="float")||(TypeInfo=="system.double"))
  120. {
  121. return SqlDbType.Float;
  122. }
  123. if(TypeInfo=="image")
  124. {
  125. return SqlDbType.Image;
  126. }
  127. if((TypeInfo=="int")||(TypeInfo=="system.int32"))
  128. {
  129. return SqlDbType.Int;
  130. }
  131. if((TypeInfo=="money")||(TypeInfo=="system.decimal"))
  132. {
  133. return SqlDbType.Money;
  134. }
  135. if(TypeInfo=="nchar")
  136. {
  137. return SqlDbType.NChar;
  138. }
  139. if(TypeInfo=="ntext")
  140. {
  141. return SqlDbType.NText;
  142. }
  143. if(TypeInfo=="nvarchar")
  144. {
  145. return SqlDbType.NVarChar;
  146. }
  147. if((TypeInfo=="real")||(TypeInfo=="system.single"))
  148. {
  149. return SqlDbType.Real;
  150. }
  151. if(TypeInfo=="smalldatetime")
  152. {
  153. return SqlDbType.SmallDateTime;
  154. }
  155. if((TypeInfo=="smallint")||(TypeInfo=="system.int16"))
  156. {
  157. return SqlDbType.SmallInt;
  158. }
  159. if(TypeInfo=="smallmoney")
  160. {
  161. return SqlDbType.SmallMoney;
  162. }
  163. if(TypeInfo=="text")
  164. {
  165. return SqlDbType.Text;
  166. }
  167. if((TypeInfo=="timestamp")||(TypeInfo=="system.timespan"))
  168. {
  169. return SqlDbType.Timestamp;
  170. }
  171. if((TypeInfo=="tinyint")||(TypeInfo=="system.byte"))
  172. {
  173. return SqlDbType.TinyInt;
  174. }
  175. if((TypeInfo=="uniqueidentifier")||(TypeInfo=="system.guid"))
  176. {
  177. return SqlDbType.UniqueIdentifier;
  178. }
  179. if(TypeInfo=="varbinary")
  180. {
  181. return SqlDbType.VarBinary;
  182. }
  183. if(TypeInfo=="variant")
  184. {
  185. return SqlDbType.Variant;
  186. }
  187. return SqlDbType.VarChar;
  188. }// 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 方法的查询模式,进行比对操作。示例代码如下:

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

使用保存模式

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

  1. string user_cid_value="1F044A84-9154-466B-9B9F-894282625729";
  2. 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 类,我们可以根据实际的需要进行使用和改造。感谢您的阅读,希望本文能够对您有所帮助。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/890729?site
推荐阅读
相关标签
  

闽ICP备14008679号