Form binding class, use reflection to bind entity classes and controls

Source: Internet
Author: User

Using system; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. htmlcontrols; <br/> using system. web. UI. webcontrols; <br/> using system. reflection; </P> <p> // ============================< br/>/ /author: dy <br/> // Date: 20080329 <br/> // ==========================< br/> namespace BLL <BR/>{< br/> /// <summary> <br/> // description: use reflection to bind business objects and controls <br/> // this class has two static methods <see CREF = "bindobjecttocontro Ls "/> and <see CREF =" bindcontrolstoobject "/> <br/> // used to bind control values or object attributes <see CREF =" control "/> <br/> /// the Control ID must be the same as the property name of the Business Object <br/> /// </Summary> <br/> public class formbinding <br />{< br/> /// <summary> <br/> // bind the object property value to the control with the same name <see CREF = "control"/> <br/> /// </Summary> <br/> /// <Param name = "OBJ"> property: the object to which the form control is bound </param> <br/> // /<Param name = "Container"> the control container is usually a page </param> <br/> Public sta TIC void bindobjecttocontrols (Object OBJ, control container) <br/>{< br/> If (OBJ = NULL) return; </P> <p> // obtain the business object attributes <br/> // <br/> type objtype = obj. getType (); <br/> propertyinfo [] objpropertiesarray = objtype. getproperties (); </P> <p> foreach (propertyinfo objproperty in objpropertiesarray) <br/>{</P> <p> control = container. findcontrol (objproperty. name); </P> <p> // list control (dropdownlist, c Heckboxlist, radiobuttonlist) <br/> // <br/> If (control is listcontrol) <br/>{< br/> listcontrol = (listcontrol) control; <br/> foreach (listitem litem in listcontrol. items) <br/>{< br/> litem. selected = false; <br/>}< br/> string propertyvalue = objproperty. getvalue (OBJ, null ). tostring (); <br/> string [] S = propertyvalue. split (','); <br/> foreach (string I in S) <br/>{< br/> listi TEM listitem = listcontrol. Items. findbytext (I); <br/> If (listitem! = NULL) listitem. Selected = true; <br/>}</P> <p >}< br/> else if (control! = NULL) <br/>{< br/> // obtain control attributes <br/> // <br/> type controltype = control. getType (); <br/> propertyinfo [] controlpropertiesarray = controltype. getproperties (); </P> <p> // <br/> // test common attributes <br/> bool success = false; <br/> success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "checked", typeof (bool); </P> <p> If (! Success) <br/> success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "selecteddate", typeof (datetime); </P> <p> If (! Success) <br/> success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "value", typeof (string); </P> <p> If (! Success) <br/> success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "text", typeof (string); </P> <p> If (! Success) <br/> success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "innerhtml", typeof (string )); </P> <p >}< br/>}</P> <p> // <summary> <br/> // query Control attribute and type, and try to assign a value to the property of the same name of the Business Object <br/> /// </Summary> <br/> // <Param name = "OBJ"> the property is found. </param> <br/> /// <Param name = "objproperty"> Properties of the object </param> <br/> // <Param name = "control"> control that matches the Control ID and Business Object Property name </param> <br/> /// <Param name = "controlpropertiesarray"> Control attribute set </param> <br/> // <Param name = "propertyname"> name of the control attribute to be set </param> <br/> /// <Param name = "type"> control property type correction </param> <br/> /// <returns> whether the property is found and set </returns> <br/> Private Static bool findandsetcontrolproperty (Object OBJ, propertyinfo objproperty, control, propertyinfo [] controlpropertiesarray, string propertyname, type) <br/>{< br/> // Traverse Control attributes <br/> // <Br/> foreach (propertyinfo controlproperty in controlpropertiesarray) <br/>{< br/> // check the property name and type <br/> // <br/> If (controlproperty. name = propertyname & controlproperty. propertytype = type) <br/>{< br/> // set the value of the control property to the Business Object <br/> // <br/> controlproperty. setvalue (control, convert. changetype (objproperty. getvalue (OBJ, null), type), null); <br/> return true; <br/>}< br/> return false; <Br/>}</P> <p> // <summary> <br/> // set the S Value of the control to business. <see CREF = "control"/> object <br/> /// </Summary> <br/> // <Param name = "OBJ"> business object to be bound </param> <br/> /// <Param name = "Container"> Control container </param> <br/> Public static void bindcontrolstoobject (Object OBJ, control container) <br/>{< br/> If (OBJ = NULL) return; </P> <p> // obtain the attributes of a Business Object <br/> // <br/> type objtype = obj. getType (); <br/> propertyinfo [] objpropertie Sarray = objtype. getproperties (); </P> <p> foreach (propertyinfo objproperty in objpropertiesarray) <br/>{</P> <p> control = container. findcontrol (objproperty. name); <br/> If (control is listcontrol) <br/>{< br/> listcontrol = (listcontrol) control; <br/> string STR = ""; <br/> If (listcontrol. selecteditem! = NULL) </P> <p> for (INT I = 0; I <listcontrol. items. count; I ++) <br/>{< br/> If (listcontrol. items [I]. selected) <br/>{< br/> STR + = listcontrol. items [I]. text + ","; <br/>}< br/> objproperty. setvalue (OBJ, convert. changetype (STR, objproperty. propertytype), null); </P> <p >}< br/> else if (control is htmlinputhidden) <br/>{</P> <p> try <br/>{< br/> htmlinputhidden hidcontrol = (htmlinputhidde N) control; <br/> // hidcontrol. value = hidcontrol. value = ""? "0": hidcontrol. value; <br/> objproperty. setvalue (OBJ, convert. changetype (hidcontrol. value, objproperty. propertytype), null ); <br/>}< br/> catch <br/>{</P> <p >}< br/> else if (control is htmlinputtext) <br/>{</P> <p> try <br/>{< br/> htmlinputtext txtcontrol = (htmlinputtext) control; <br/> // txtcontrol. value = txtcontrol. value = ""? "0": txtcontrol. value; <br/> objproperty. setvalue (OBJ, convert. changetype (txtcontrol. value, objproperty. propertytype), null ); <br/>}< br/> catch <br/>{</P> <p >}< br/>}</P> <p> else if (control! = NULL) <br/>{< br/> // obtain control attributes <br/> // <br/> type controltype = control. getType (); <br/> propertyinfo [] controlpropertiesarray = controltype. getproperties (); </P> <p> // try common attributes <br/> // <br/> bool success = false; <br/> success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "checked", typeof (bool); </P> <p> If (! Success) <br/> success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "selecteddate", typeof (datetime); </P> <p> If (! Success) <br/> success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "value", typeof (string); </P> <p> If (! Success) <br/> success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "text", typeof (string )); <br/>}</P> <p> /// <summary> <br/> // query the control value and try assign a value to the Business Object Property of the same name <br/> /// </Summary> <br/> /// <Param name = "OBJ"> to be business Object </param> <br/> // <Param name = "objproperty"> Business Object Attributes </param> <br/> // <Param name = "control"> controls with the same name </param> <br/> // <Param name = "controlpropertiesarray"> control array </param> <br/> // <param name = "propertyname"> the obtained control property name </param> <br/> // <Param name = "type"> control property type </param> <br />/// <returns> whether the property is found </returns> <br/> Private Static bool findandgetcontrolproperty (Object OBJ, propertyinfo objproperty, control, propertyinfo [] controlpropertiesarray, string propertyname, type) <br/> {<br/> // Traverse Control attributes <br/> // <br/> foreach (propertyinfo controlproperty in controlpropertiesarray) <br/>{< br/> // check the name and type <br/> // <br/> If (controlproperty. name = propertyname & controlproperty. propertytype = typeof (string )) <br/> {<br/> // control is assigned to the Business Object <br/> // <br/> try <br/> {<br/> Object ctrvalue = convert. changetype (controlproperty. getvalue (control, null), objproperty. propertytype); <br/> objproperty. setvalue (OBJ, ctrvalue, null); </P> <p> return true; <br/>}< br/> catch <br/> {<br/> // The Business Object Property corresponding to the control is not found. <br/> return false; <br/>}< br/> return false; <br/>}< br/>

The attributes of the object class are the same as those of the control ID.

Call time

Assign a value to the control by the object class: Bll. formbinding. bindobjecttocontrols (model, this); // model is the object class.

Value. The value of the control is assigned to the object class: Bll. formbinding. bindcontrolstoobject (model, this );

Complete the value and value assignment in one sentence.

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.