BaisePage.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Reflection;
  7. namespace Ant.Frame
  8. {
  9. public class BaisePage : Page
  10. {
  11. /// <summary>
  12. /// 从页面读取控件值,并赋值给实体。
  13. /// 引用实例Report_ProformaInvoiceAudit factoryMonthlyReport = new Report_ProformaInvoiceAudit();
  14. /// factoryMonthlyReport = Getter<Report_ProformaInvoiceAudit>(factoryMonthlyReport);
  15. /// </summary>
  16. /// <typeparam name="T">实体类型< peparam>
  17. /// <param name="entity">实体的一个实例</param>
  18. /// <returns></returns>
  19. public T Getter<T>(T entity)
  20. {
  21. TextBox currentTextBox = null;
  22. foreach (Control cp in Page.Controls)
  23. {
  24. foreach (Control ct in cp.Controls)
  25. {
  26. if (ct is System.Web.UI.HtmlControls.HtmlForm)
  27. {
  28. foreach (Control con in ct.Controls)
  29. {
  30. foreach (Control control in con.Controls)
  31. {
  32. if (control is TextBox && control.ClientID.Contains("txt"))
  33. {
  34. //
  35. currentTextBox = (TextBox)control;
  36. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  37. PropertyInfo currentInfo = entity.GetType().GetProperty(fieldName);
  38. object d;
  39. //if (((System.Reflection.MemberInfo)(currentInfo.PropertyType)).Name.Contains("String"))
  40. if (currentInfo.PropertyType.Name.Contains("String"))
  41. {
  42. d = currentTextBox.Text;
  43. }
  44. else
  45. {
  46. if (currentInfo.PropertyType.FullName.Contains("Decimal"))
  47. {
  48. d = decimal.Parse(currentTextBox.Text);
  49. }
  50. else
  51. {
  52. d = int.Parse(currentTextBox.Text);
  53. }
  54. }
  55. currentInfo.SetValue(entity, d, null);
  56. //entity.GetType().InvokeMember(fieldName, BindingFlags.SetProperty, null, entity, new object[] { currentTextBox.Text });
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. return entity;
  64. }
  65. /// <summary>
  66. /// 将实体展现到页面控件中。
  67. /// Setter<Report_ProformaInvoiceAudit>(factoryMonthlyReport);
  68. /// </summary>
  69. /// <typeparam name="T">实体类型</typeparam>
  70. /// <param name="entity">从数据库中查找出的实体的实例</param>
  71. public void Setter<T>(T entity)
  72. {
  73. TextBox currentTextBox = null;
  74. foreach (Control cp in Page.Controls)
  75. {
  76. foreach (Control ct in cp.Controls)
  77. {
  78. if (ct is System.Web.UI.HtmlControls.HtmlForm)
  79. {
  80. foreach (Control con in ct.Controls)
  81. {
  82. foreach (Control control in con.Controls)
  83. {
  84. if (control is TextBox && control.ClientID.Contains("txt"))
  85. {
  86. currentTextBox = (TextBox)control;
  87. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  88. currentTextBox.Text = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }