123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Reflection;
- namespace Ant.Frame
- {
- public class BaisePage : Page
- {
- /// <summary>
- /// 从页面读取控件值,并赋值给实体。
- /// 引用实例Report_ProformaInvoiceAudit factoryMonthlyReport = new Report_ProformaInvoiceAudit();
- /// factoryMonthlyReport = Getter<Report_ProformaInvoiceAudit>(factoryMonthlyReport);
- /// </summary>
- /// <typeparam name="T">实体类型< peparam>
- /// <param name="entity">实体的一个实例</param>
- /// <returns></returns>
- public T Getter<T>(T entity)
- {
- TextBox currentTextBox = null;
- foreach (Control cp in Page.Controls)
- {
- foreach (Control ct in cp.Controls)
- {
- if (ct is System.Web.UI.HtmlControls.HtmlForm)
- {
- foreach (Control con in ct.Controls)
- {
- foreach (Control control in con.Controls)
- {
- if (control is TextBox && control.ClientID.Contains("txt"))
- {
- //
- currentTextBox = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- PropertyInfo currentInfo = entity.GetType().GetProperty(fieldName);
- object d;
- //if (((System.Reflection.MemberInfo)(currentInfo.PropertyType)).Name.Contains("String"))
- if (currentInfo.PropertyType.Name.Contains("String"))
- {
- d = currentTextBox.Text;
- }
- else
- {
- if (currentInfo.PropertyType.FullName.Contains("Decimal"))
- {
- d = decimal.Parse(currentTextBox.Text);
- }
- else
- {
- d = int.Parse(currentTextBox.Text);
- }
- }
- currentInfo.SetValue(entity, d, null);
- //entity.GetType().InvokeMember(fieldName, BindingFlags.SetProperty, null, entity, new object[] { currentTextBox.Text });
- }
- }
- }
- }
- }
- }
- return entity;
- }
- /// <summary>
- /// 将实体展现到页面控件中。
- /// Setter<Report_ProformaInvoiceAudit>(factoryMonthlyReport);
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="entity">从数据库中查找出的实体的实例</param>
- public void Setter<T>(T entity)
- {
- TextBox currentTextBox = null;
- foreach (Control cp in Page.Controls)
- {
- foreach (Control ct in cp.Controls)
- {
- if (ct is System.Web.UI.HtmlControls.HtmlForm)
- {
- foreach (Control con in ct.Controls)
- {
- foreach (Control control in con.Controls)
- {
- if (control is TextBox && control.ClientID.Contains("txt"))
- {
- currentTextBox = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- currentTextBox.Text = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
- }
- }
- }
- }
- }
- }
- }
- }
- }
|