using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Collections;
namespace WIZARD.Utils
{ public class ObjectUISync { /// <summary> /// 邦定界面控件到業務對象 /// </summary> /// <param name="p">界面控件容器</param> /// <param name="o">實體類</param> public static void Bind(Control p,object o) { Type type = o.GetType(); PropertyInfo[] infos = type.GetProperties();
Control nc = null; foreach (PropertyInfo info in infos) { string infoname = info.Name; nc = p.FindControl(infoname); if (nc == null) { foreach (Control c in p.Controls) { nc = null; if (!c.HasControls()) continue; nc = c.FindControl(infoname); if (nc != null) break; } } if (nc != null) { try { string v = null; switch (nc.GetType().Name) { case "TextBox": v = ((TextBox)nc).Text.ToString(); break; case "Label": v = ((Label)nc).Text.ToString(); break; case "CheckBox": v = ((CheckBox)nc).Checked.ToString(); break; case "Radio": v = ((CheckBox)nc).Checked.ToString(); break; case "Image": v = ((Image)nc).ImageUrl.ToString(); break; case "DropDownList": v = ((DropDownList)nc).SelectedValue; break; case "LinkButton": v = ((LinkButton)nc).Text; break; case "HyperLink": v = ((HyperLink)nc).Text; break; case "RadioButtonList": v = ((RadioButtonList)nc).SelectedValue; break; case "CheckBoxList": ListItemCollection items = ((CheckBoxList)nc).Items; foreach (ListItem item in items) { if (v == null) { if (item.Selected) v = item.Value; } else { if (item.Selected) v += "," + item.Value; } } break; case "ListBox": v = ((RadioButtonList)nc).SelectedValue; break; } info.SetValue(o, Convert.ChangeType(v, info.PropertyType), null); } catch (System.Exception) { } } }
}
/// <summary> /// 邦定業務對象到界面控件 /// </summary> /// <param name="p">界面控件容器</param> /// <param name="o">實體類</param> public static void Show(Control p,object o) { Type type = o.GetType(); PropertyInfo[] infos = type.GetProperties();
for (int k = 0; k < infos.Length; k++) { PropertyInfo info = infos[k]; Control nc = null; string infoname = info.Name; nc = p.FindControl(infoname); if (nc == null) { foreach (Control c in p.Controls) { nc = null; if (!c.HasControls()) continue; nc = c.FindControl(infoname); if (nc != null) break; } } if (nc != null) { try { object v = info.GetValue(o, null); string typeName = ""; typeName = nc.GetType().Name; switch (typeName) { case "TextBox": ((TextBox)nc).Text = v.ToString(); break; case "Label": ((Label)nc).Text = v.ToString(); break; case "CheckBox": ((CheckBox)nc).Checked = bool.Parse(v.ToString()); break; case "Radio": ((CheckBox)nc).Checked = bool.Parse(v.ToString()); break; case "Image": ((Image)nc).ImageUrl = v.ToString(); break; case "DropDownList": ((DropDownList)nc).SelectedValue = v.ToString(); break; case "LinkButton": ((LinkButton)nc).Text = v.ToString(); break; case "HyperLink": ((HyperLink)nc).Text = v.ToString(); break; case "RadioButtonList": ((RadioButtonList)nc).SelectedValue = v.ToString(); break; case "CheckBoxList": string[] ps = v.ToString().Split(','); ListItemCollection items = ((CheckBoxList)nc).Items; //bool first = false; foreach (string key in ps) { ListItem item = null; item = items.FindByValue(key); if (item != null) item.Selected = true;