123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using System;
- using System.Text;
- using System.Web.UI.WebControls;
- using System.Web.UI;
- using Win = System.Windows.Forms;
- using Ant.ORM.Table;
- using System.Collections.Generic;
- using Ant.ORM.SQL;
- using System.Data;
- using System.ComponentModel;
- namespace Ant.ORM
- {
- internal class MActionUI:IDisposable
- {
- private List<string> autoPrefixList;//调用插入和更新,自动获取控件名的前缀
- 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 = ct.ID.Substring(3);
- if (value == null)
- {
- value = _Row[propName].Value;
- }
- switch (ct.GetType().Name)
- {
- case "TextBox":
- ((TextBox)ct).Text = Convert.ToString(value);
- ((TextBox)ct).Enabled = 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 "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;
- }
- }
- public void GetFrom(Control ct, object value)
- {
- string propName = ct.ID.Substring(3);
- if (value == null)
- {
- switch (ct.GetType().Name)
- {
- case "TextBox":
- value = ((TextBox)ct).Text.Trim();
- break;
- case "Literal":
- value = ((Literal)ct).Text;
- break;
- case "Label":
- value = ((Label)ct).Text;
- break;
- case "HiddenField":
- value = ((HiddenField)ct).Value;
- 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 = 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 = 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 Web自动取值
- /// <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)
- {
- 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
- {
-
- }
- }
- }
- }
- #endregion
- #region 其它方法
- public void SetAutoPrefix(string autoPrefix, params string[] otherPrefix)
- {
- autoPrefixList = new List<string>();
- 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
- }
- }
|