cyq.data的自動賦值取值都是依據控件前三位後邊的爲字段依據,修改了一下。改爲若是不使用SetAutoPrefix方法,則自動獲取和字段一一對應的數據,使用SetAutoPrefix方法後才使用指定的前綴。數據庫
protected void Page_Load(object sender, EventArgs e) { UserName.Text = ""; UserPwd.Text = ""; RealName.Text = ""; Email.Text = ""; Mobile.Text = ""; MAction action = new MAction("Base_Admin"); action.Fill("id=1"); action.SetToAll(this); action.Close(); } protected void Button1_Click(object sender, EventArgs e) { MAction action = new MAction("Base_Admin"); //action.Set("username", "123123"); action.Insert(true); action.Close(); }
上邊這種是把數據直接綁定到頁面,控件ID和數據庫字段一一對應。再新增插入的時候也無須指定控件前綴,默認取數據庫字段名。this
protected void Page_Load(object sender, EventArgs e) { UserName.Text = ""; UserPwd.Text = ""; RealName.Text = ""; Email.Text = ""; Mobile.Text = ""; MAction action = new MAction("Base_Admin"); action.Fill("id=1"); action.SetAutoPrefix("txt","ddl"); action.SetToAll(this); action.Close(); } protected void Button1_Click(object sender, EventArgs e) { MAction action = new MAction("Base_Admin"); //action.Set("username", "123123"); action.SetAutoPrefix("txt","ddl"); action.Insert(true); action.Close(); }
這種是指定了控件前綴,把全部已txt、ddl開頭連上數據庫字段的控件綁定,插入的時候插入txt、ddl開頭的和數據庫字段對應的數據。spa
這樣若是字段和控件直接對應的狀況,就不用考慮SetAutoPrefix方法了。用了SetAutoPrefix方法,則和之前的cyq同樣使用。orm
using System; using System.Text; using System.Web.UI.WebControls; using System.Web.UI; using Win = System.Windows.Forms; using CYQ.Data.Table; using System.Collections.Generic; using CYQ.Data.SQL; using System.Data; using System.ComponentModel; using System.Web.UI.HtmlControls; namespace CYQ.Data { internal class MActionUI : IDisposable { private List<string> autoPrefixList = new List<string>() { "" };//調用插入和更新,自動獲取控件名的前綴 public MDataRow _Row; public MActionUI(ref MDataRow row) { _Row = row; } #region UI操做分路 public void Set(object ct, object value, bool isControlEnabled) { if (ct is Control) { SetTo(ct as Control, value, isControlEnabled); } else { SetTo(ct as Win.Control, value, isControlEnabled); } } public void Get(object ct, object value) { if (ct is Control) { GetFrom(ct as Control, value); } else { GetFrom(ct as Win.Control, value); } } #endregion #region WebUI操做 public void SetTo(Control ct, object value, bool isControlEnabled) { string propName = string.Empty; if (string.IsNullOrEmpty(autoPrefixList[0])) { propName = ct.ID; } else { propName = ct.ID.Substring(3); } if (value == null) { value = _Row[propName].Value; } switch (ct.GetType().Name) { case "HtmlInputText": ((HtmlInputText)ct).Value = Convert.ToString(value); ((HtmlInputText)ct).Disabled = !isControlEnabled; break; case "HtmlSelect": ((HtmlSelect)ct).Value = Convert.ToString(value); ((HtmlSelect)ct).Disabled = !isControlEnabled; break; case "HtmlInputHidden": ((HtmlInputHidden)ct).Value = Convert.ToString(value); break; case "HtmlInputPassword": ((HtmlInputPassword)ct).Value = Convert.ToString(value); ((HtmlInputPassword)ct).Disabled = !isControlEnabled; break; case "HtmlTextArea": ((HtmlTextArea)ct).Value = Convert.ToString(value); ((HtmlTextArea)ct).Disabled = !isControlEnabled; break; case "Literal": ((Literal)ct).Text = Convert.ToString(value); break; case "Label": ((Label)ct).Text = Convert.ToString(value); break; case "HiddenField": ((HiddenField)ct).Value = Convert.ToString(value); break; case "TextBox": ((TextBox)ct).Text = Convert.ToString(value); ((TextBox)ct).Enabled = isControlEnabled; break; case "DropDownList": ((DropDownList)ct).SelectedValue = Convert.ToString(value); ((DropDownList)ct).Enabled = isControlEnabled; break; case "CheckBox": bool tempValue; if (Convert.ToString(value) == "1") { tempValue = true; } else { bool.TryParse(Convert.ToString(value), out tempValue); } ((CheckBox)ct).Checked = tempValue; ((CheckBox)ct).Enabled = isControlEnabled; break; } } /// <summary> /// 批量設置值 /// </summary> public void SetToAll(object page, bool isControlEnabled) { MDataColumn mdc = _Row.Columns; for (int i = 0; i < mdc.Count; i++) { string key = mdc[i].ColumnName; //獲取到列名 string value = _Row[key].Value.ToString(); //對應值 if (page is Control) { foreach (string autoPrefix in autoPrefixList) { Control control = ((Control)page).FindControl(autoPrefix + key); if (control != null) { SetTo(control, value, isControlEnabled); } } } else { foreach (string autoPrefix in autoPrefixList) { Win.Control control = this.findControl((Win.Control)page, autoPrefix + key); if (control != null) { SetTo(control, value, isControlEnabled); } } } } } /// <summary> /// 獲取值 /// </summary> public void GetFrom(Control ct, object value) { string propName = string.Empty; if (string.IsNullOrEmpty(autoPrefixList[0])) { propName = ct.ID; } else { propName = ct.ID.Substring(3); } if (value == null) { switch (ct.GetType().Name) { case "HtmlInputText": value = ((HtmlInputText)ct).Value; break; case "HtmlSelect": value = ((HtmlSelect)ct).Value; break; case "HtmlInputHidden": value = ((HtmlInputHidden)ct).Value; break; case "HtmlInputPassword": value = ((HtmlInputPassword)ct).Value; break; case "HtmlTextArea": value = ((HtmlTextArea)ct).Value; break; case "Literal": value = ((Literal)ct).Text; break; case "Label": value = ((Label)ct).Text; break; case "HiddenField": value = ((HiddenField)ct).Value; break; case "TextBox": value = ((TextBox)ct).Text.Trim(); break; case "DropDownList": value = ((DropDownList)ct).SelectedValue; break; case "CheckBox": value = ((CheckBox)ct).Checked; break; } } _Row[propName].Value = value; } #endregion #region WinUI操做 public void SetTo(Win.Control ct, object value, bool isControlEnabled) { string propName = string.Empty; if (string.IsNullOrEmpty(autoPrefixList[0])) { propName = ct.Name; } else { propName = ct.Name.Substring(3); } if (value == null) { value = _Row[propName].Value; } switch (ct.GetType().Name) { case "TextBox": ((Win.TextBox)ct).Text = Convert.ToString(value); ((Win.TextBox)ct).Enabled = isControlEnabled; break; case "ComboBox": ((Win.ComboBox)ct).Items.Add(value); break; case "Label": ((Win.Label)ct).Text = Convert.ToString(value); break; case "DateTimePicker": DateTime dt; if (DateTime.TryParse(Convert.ToString(value), out dt)) { ((Win.DateTimePicker)ct).Value = dt; } break; case "ListBox": ((Win.ListBox)ct).Items.Add(value); break; case "CheckBox": bool tempValue; if (Convert.ToString(value) == "1") { tempValue = true; } else { bool.TryParse(Convert.ToString(value), out tempValue); } ((Win.CheckBox)ct).Checked = tempValue; ((Win.CheckBox)ct).Enabled = isControlEnabled; break; case "NumericUpDown": decimal result = 0; if (decimal.TryParse(Convert.ToString(value), out result)) { ((Win.NumericUpDown)ct).Value = result; } break; case "RichTextBox": ((Win.ListBox)ct).Text = Convert.ToString(value); break; } } public void GetFrom(Win.Control ct, object value) { string propName = string.Empty; if (string.IsNullOrEmpty(autoPrefixList[0])) { propName = ct.Name; } else { propName = ct.Name.Substring(3); } if (value == null) { switch (ct.GetType().Name) { case "TextBox": value = ((Win.TextBox)ct).Text.Trim(); break; case "ComboBox": value = ((Win.ComboBox)ct).Text; break; case "Label": value = ((Win.Label)ct).Text; break; case "DateTimePicker": value = ((Win.DateTimePicker)ct).Value; break; case "ListBox": value = ((Win.ListBox)ct).Text; break; case "CheckBox": value = ((Win.CheckBox)ct).Checked; break; case "NumericUpDown": value = ((Win.NumericUpDown)ct).Value; break; case "RichTextBox": value = ((Win.RichTextBox)ct).Text; break; } } _Row[propName].Value = value; } #endregion #region 自動取值 /// <summary> /// 自動設置列的值(true爲插入,false爲更新) /// </summary> public void AutoSetColumnValue(bool containsID) { // Type type = null; int i = 0; if (containsID || !_Row[0]._CellValue.IsNull) { i = 1; } for (; i < _Row.Count; i++) { if (!_Row[i]._CellValue.IsChange) { try { foreach (string autoPrefix in autoPrefixList) { if (System.Web.HttpContext.Current == null) //win批量取值 { //待實現 } else { string RequestValue = System.Web.HttpContext.Current.Request[autoPrefix + _Row[i]._CellStruct.ColumnName]; if (RequestValue != null) { if (RequestValue == "on") { if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit) { _Row[i].Value = true; } else { _Row[i].Value = 1; } break; } if (RequestValue.Length == 0 && DataType.GetGroupID(_Row[i]._CellStruct.SqlType) == 1) { _Row[i].Value = 0; break; } else if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit && RequestValue.Length == 1) { _Row[i].Value = RequestValue == "1"; } _Row[i].Value = TypeDescriptor.GetConverter(_Row[i]._CellStruct.ValueType).ConvertFrom(RequestValue.Trim()); break; } } } } catch (Exception e) { throw new Exception(e.Message); } } } } #endregion #region 其它方法 public void SetAutoPrefix(string autoPrefix, params string[] otherPrefix) { //autoPrefixList = new List<string>(); autoPrefixList.Clear(); //清空默認值 autoPrefixList.Add(autoPrefix); foreach (string item in otherPrefix) { autoPrefixList.Add(item); } } #endregion #region IDisposable 成員 public void Dispose() { if (autoPrefixList != null) { autoPrefixList.Clear(); autoPrefixList = null; } } #endregion #region 新增查找控件 private Win.Control findControl(Win.Control control, string controlName) { Win.Control c1; foreach (Win.Control c in control.Controls) { if (c.Name == controlName) { return c; } else if (c.Controls.Count > 0) { c1 = findControl(c, controlName); if (c1 != null) { return c1; } } } return null; } #endregion } }
裏邊winform的批量賦值也已經一併修改好啦!blog