asp.net 信息管理系統中的需填寫數據的鍵值對獲取方法

問題描述:

在信息管理系統中,通常要採集大量的數據,然後把數據寫入數據庫中,如何進行一次輸入數據庫對應字段,可以在數據的採集,以及數據的回顯

階段都能使用呢?當然,是能做到的。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();
        }
    }





發佈了27 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章