There is a problem: the name of the form and the object's property name (I am a property assignment you can also use the field) to keep the same, a bit insecure, but the background is very good, in the said fill out form data background with more
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Collections.Generic;
Using System.Reflection;
Using System.Collections.Specialized;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
<summary>
Get form values from Object settings
</summary>
Namespace Com.fun
{
public static Class Setformtomodel<t>
{
<summary>
Assign a form to an object
</summary>
<param name= "T" > Entity Objects </param>
<param name= "Form" > Form collection </param>
public static void GetValue (T T, namevaluecollection form)
{
Type type = T.gettype ();
propertyinfo[] pi = type. GetProperties ();
foreach (PropertyInfo p in pi)
{
if (Form[p.name]!= null)
{
P.setvalue (t, Convert.changetype (Form[p.name], p.propertytype), NULL);
}
}
}
<summary>
Assigning an object to a form
</summary>
<param name= "T" > Entity Objects </param>
<param name= "C" > Page objects </param>
public static void SetValue (T t,page Page)
{
Type type = T.gettype ();
propertyinfo[] pi = type. GetProperties ();
foreach (PropertyInfo p in pi)
{
System.Web.UI.HtmlControls.HtmlInputText Text = page. FindControl (p.name) as System.Web.UI.HtmlControls.HtmlInputText;
if (text!= null)
{
Text. Value = p.getvalue (t, NULL). ToString ();
}
}
}
}
}
Call
Mhousereco MH = new Dhousereco (). Getmodel (ID);
Com.fun.setformtomodel<mhousereco>. SetValue (mh,this. Page);
Mhousereco MH = new Mhousereco ();
Com.fun.setformtomodel<mhousereco>. GetValue (MH, this. Request.Form);