Implementation code for entity class object assignment to form in asp.net

Source: Internet
Author: User
Tags foreach object net static class
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 ();
}
}

}
}
}


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);


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.