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 CodeThe code is 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 ();
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.