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和数据库字段一一对应。再新增插入的时候也无须指定控件前缀,默认取数据库字段名。
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开头的和数据库字段对应的数据。
这样如果字段和控件直接对应的情况,就不用考虑SetAutoPrefix方法了。用了SetAutoPrefix方法,则和以前的cyq一样使用。
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的批量赋值也已经一并修改好啦!
浙公网安备 33010602011771号