Object assigned to form asp.net code

Source: Internet
Author: User

The following is the implementation code for assigning a value to a form for an object class in asp.net. A friend told me a few days ago that the reflection form assigned a value to an object method of an object class. Below is a very good example.

The following is an asp tutorial. net object assignment to form implementation code, a friend told me a few days ago reflection form assignment to an object method of an object class, below 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.html controls;
/// <Summary>
/// Obtain the form value through object settings
/// </Summary>
Namespace com. fun
{
Public static class setformtomodel <t>
{
/// <Summary>
/// Grant the form to the object
/// </Summary>
/// <Param name = "t"> Object </param>
/// <Param name = "form"> form set </param>
Public static void getvalue (t, namevaluecollection form)
{
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>
/// Assign the object to the form
/// </Summary>
/// <Param name = "t"> Object </param>
/// <Param name = "c"> Page Object </param>
Public static void setvalue (t, page)
{
Type = t. gettype ();
Propertyinfo [] pi = type. getproperties ();
Foreach (propertyinfo p in pi)
{
System.web.ui.htmlcontrols.html inputtext text = page. findcontrol (p. name) as system.web.ui.htmlcontrols.html inputtext;
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.