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
原创粉丝点击