Object assignment to form asp.net code

Source: Internet
Author: User
Tags reflection

The following is an ASP tutorial. NET Entity class object assignment to form implementation code, a few days ago a friend told me reflection form assignment to an object method of an entity class, the following is a very good example.

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 through object settings
///</summary>
Namespace Com.fun
{
public static C Lass setformtomodel<t>
{
///<summary>
///assigns a form to an object
///</summary>
///<para M 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 (for M[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.