asp.net 信息管理系统中的需填写数据的键值对获取方法
来源:互联网 发布:硕鼠 youtube mac 编辑:程序博客网 时间:2024/05/23 21:22
问题描述:
在信息管理系统中,通常要采集大量的数据,然后把数据写入数据库中,如何进行一次输入数据库对应字段,可以在数据的采集,以及数据的回显
阶段都能使用呢?当然,是能做到的。asp.net中C#方法如下:
首先,对应数据库字段的名称吗,设置页面可输入元素的ID,并且区分每种控件的类型,大致如:<asp:Label runat=server ID="lb_proName" ></asp:Label>,其中Lable就用lb_字段名,加以区分;同样的tb_字段名(textbox),rbl_字段名(radiobuttonlist),等诸如之类的标识。注意:字段名一定要与数据库中的字段名一致。
然后,循环获取到页面所有可输入元素对应的键值对。
//获取所有的标识控件的键值对 private void GetAllControlKeyVals(Control c, Dictionary<string, string> TableDic, ref ArrayList ctrArray) { if (c is WebControl) { WebControl ctr = (WebControl)c; if (ctr.ID != null) { if (ctr.ID.Contains("tb_") && !ctr.ID.Contains("_none_")) { TableDic[ctr.ID] = ((TextBox)ctr).Text; ctrArray.Add(ctr); } if (ctr.ID.Contains("lb_") && !ctr.ID.Contains("_none_")) { TableDic[ctr.ID] = ((Label)ctr).Text; ctrArray.Add(ctr); } if (ctr.ID.Contains("rbl_") && !ctr.ID.Contains("_none_")) { TableDic[ctr.ID] = ((RadioButtonList)ctr).SelectedValue; ctrArray.Add(ctr); } } } if (c.HasControls() == false) { return; } foreach (Control child in c.Controls) { GetAllControlKeyVals(child, TableDic, ref ctrArray); } }通过以上的公用方法,传入要检索的控件对象,这里一般是使用this.Page,也就是检索整个页面对象。一个字典对象返回,可输入元素的键值对,arrayList对象返回,每个键值对对应的那个控件对象,两者是一种对应关系。
最后是数据的回显,包括查看时,textbox字段的显示样式设置。这里比较简单,不做细致讲解。
//控制查看视图 private void CtrEnableAndShow() { string type = Request["type"].ToString(); //MyClientScript.ygJScript.Alert(type, this); if ("View" == type) { //保存和提交按钮不显示 btn_Save.Visible = false; btn_Submit.Visible = false; ArrayList ctrArray = new ArrayList(); Dictionary<string, string> dic = new Dictionary<string, string>(); GetAllControlKeyVals(this, dic, ref ctrArray); int index = 0; foreach (KeyValuePair<string, string> item in dic) { if (item.Key.Contains("tb_")) { ((TextBox)ctrArray[index]).ReadOnly = true ; ((TextBox)ctrArray[index]).BackColor = System.Drawing.Color.White; ((TextBox)ctrArray[index]).BorderStyle = BorderStyle.None; } if (item.Key.Contains("rbl_")) { ((RadioButtonList)ctrArray[index]).Enabled = false; } index++; } } } //数据的回显处理 private void InitDataInfo() { string type = Request["type"].ToString(); if ("Add" != type) { string sql = "select * from tb_approval where proNum='" + lb_proNum.Text + "'"; DataTable resDt = MyDBInterface.getTableByText(sql); if (resDt != null && resDt.Rows.Count > 0) { //主键要单独的赋值 lb_none_approvalID.Text = resDt.Rows[0]["approvalID"].ToString(); //循环给其他项赋值 Dictionary<string, string> dataInfo = new Dictionary<string, string>(); ArrayList ctrArray = new ArrayList(); GetAllControlKeyVals(this, dataInfo, ref ctrArray); int index = 0;//作为arrayList的索引 foreach (KeyValuePair<string, string> item in dataInfo) { if (item.Key.Contains("lb_")) { ((Label)ctrArray[index]).Text = resDt.Rows[0][item.Key.Split('_')[1]].ToString(); } if (item.Key.Contains("tb_")) { ((TextBox)ctrArray[index]).Text = resDt.Rows[0][item.Key.Split('_')[1]].ToString(); } if (item.Key.Contains("rbl_")) { if ("" != resDt.Rows[0][item.Key.Split('_')[1]].ToString()) { ((RadioButtonList)ctrArray[index]).Items.FindByText(resDt.Rows[0][item.Key.Split('_')[1]].ToString()).Selected = true; } } index++; } } MyDBInterface.closeConn(); } }
0 0
- asp.net 信息管理系统中的需填写数据的键值对获取方法
- ASP.NET学生信息管理系统
- java中获取map的键值对方法
- asp.net 键值对取值
- asp.net获取系统已安装字体的方法
- asp.net学生信息管理系统一
- asp.net学生信息管理系统-参数设置简介
- asp.net中SqlDataReader获取数据存在的问题解决方法
- asp.net获取Post和Get数据的方法
- asp.net获取Post和Get数据的方法(Stream)
- Python 查询的数据库中的数据 关键值转换成键值对形式
- JAVA获取json中的全部键值对
- JAVA获取json中的全部键值对
- .Net 在WinForm控件ComboBox的Items中使用非数据关联的键值对数据
- asp.net简单的联系人信息管理
- asp.net 对系统服务的控制
- 对前面信息管理系统的完善
- 对前面信息管理系统的完善
- 《Linux就该这么学》 - 必读的红帽系统与红帽linux认证自学手册
- 13 个免费的 PNG 图像的优化和压缩工具
- 检查数据链是否存在的办法
- 杂谈学习的一点经验
- 射频识别技术漫谈(16)——Mifare UltraLight
- asp.net 信息管理系统中的需填写数据的键值对获取方法
- Zookeeper-Zookeeper client
- sql server 建表常用语句 (包括主键,外键 等)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十五)
- WebRTC中peerconnectclient&server 简析
- [Atom] Cannot install package behind a proxy on Win7
- java 重载和重写的区别
- EF学习小记
- 思科最简单3层配置