123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Ant.Frame
- {
- public class NetControl : BaseControl
- {
- Control control = new Control();
- public NetControl(Control con)
- {
- control = con;
- }
- /// <summary>
- /// Lable控件
- /// </summary>
- /// <param name="entity"></param>
- /// <param name="lbl"></param>
- /// <returns></returns>
- public override int SetterControl(List<object> entity, string lbl)
- {
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- Label lblcon = new Label();
- if (control is Label)
- {
- lblcon = (Label)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- foreach (object obj in entity)
- {
- if (string.IsNullOrEmpty(str))
- {
- try
- {
- str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
- }
- catch { str = ""; }
- }
- else
- break;
- }
- if (!string.IsNullOrEmpty(str))
- {
- lblcon.Text = str;
- return 0;
- }
- }
- #region TextBox
- TextBox txtcon = new TextBox();
- if (control is TextBox)
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- foreach (object obj in entity)
- {
- if (string.IsNullOrEmpty(str))
- {
- try
- {
- str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
- }
- catch { str = ""; }
- }
- else
- break;
- }
- if (!string.IsNullOrEmpty(str))
- {
- txtcon.Text = str;
- return 0;
- }
- }
- #endregion
- #region DropDownList
- DropDownList ddl = new DropDownList();
- if (control is DropDownList && control.ClientID.Contains(lbl))
- {
- ddl = (DropDownList)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- try
- {
- str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
- }
- catch { }
- if (!string.IsNullOrEmpty(str))
- {
- ddl.SelectedValue = str;
- return 0;
- }
- }
- #endregion
- #region CheckBox
- CheckBox chk = new CheckBox();
- if (control is CheckBox && control.ClientID.Contains(lbl))
- {
- chk = (CheckBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- try
- {
- str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
- }
- catch { }
- if (!string.IsNullOrEmpty(str))
- {
- if (str == "1")
- chk.Checked = true;
- if (str == "0")
- chk.Checked = false;
- if (str.ToLower() == "true")
- chk.Checked = true;
- if (str.ToLower() == "false")
- chk.Checked = false;
- return 0;
- }
- }
- #endregion
- return 0;
- }
- public override T GetterControl<T>(T entity, string lbl)
- {
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- #region TextBox
- TextBox txtcon = new TextBox(); List<T> objlist = new List<T>();
- T objj = Activator.CreateInstance<T>(); objj = entity;
- // object objj = Activator.CreateInstance(entity.GetType());
- if (control is TextBox && control.ClientID.Contains(lbl))
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- // PropertyInfo[] propertys = objj.GetType().GetProperties();
- PropertyInfo pi = entity.GetType().GetProperty(fieldName, BindingFlags.Public | BindingFlags.NonPublic
- | BindingFlags.Instance | BindingFlags.IgnoreCase);
- if (!object.Equals(pi, null))
- {
- try
- {
- if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
- pi.SetValue(objj, null, null);//为空,但不为Null
- else
- pi.SetValue(objj, txtcon.Text, null);
- }
- catch
- {
- pi.SetValue(objj, null, null);//为空,但不为Null
- }
- }
- }
- #endregion
- return objj;
- }
- public override List<object> GetterControl(List<object> entity, string lbl)
- {
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- #region TextBox
- TextBox txtcon = new TextBox(); List<object> objlist = new List<object>();
- // object objj = Activator.CreateInstance(entity.GetType());
-
- if (control is TextBox && control.ClientID.Contains(lbl))
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- // PropertyInfo[] propertys = objj.GetType().GetProperties();
- foreach (object obj in entity)
- {
- //object objj = Activator.CreateInstance(obj.GetType());
- PropertyInfo pi = obj.GetType().GetProperty(fieldName);
- if (!object.Equals(pi, null))
- {
- try
- {
- if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
- pi.SetValue(obj, null, null);//为空,但不为Null
- else
- pi.SetValue(obj, txtcon.Text, null);
- }
- catch
- {
- pi.SetValue(obj, null, null);//为空,但不为Null
- }
- objlist.Add(obj);
- }
- }
- }
- #endregion
- return objlist;
- }
- }
- }
|