Automatically assign the control value of the page to the object

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. reflection;
Using system. IO;
Using system. text;

Namespace projectdemo. Common
{
Public static class entitycopy
{

# Region
/// <Summary>

/// Create a model object by searching for the corresponding controls on the page (the control must be named by the "_ model attribute name" (for example, _ name) and be case-insensitive)

/// </Summary>

/// <Param name = "model"> model object to be constructed () </param>

/// <Param name = "parentcontrol"> Control container (such as the page or master station control) </param>

/// <Returns> return Parameter Model </returns>

Public static t GetModel <t> (T model, control parentcontrol)

{

Type T = model. GetType ();

Propertyinfo [] Pi = T. getproperties ();

Foreach (propertyinfo P in PI)

{

Setcontrolvaluetomodel (model, P, parentcontrol );

}

Return Model;

}

 

/// <Summary>

/// Assign the value on the page control to the model object (the control must be named by the "_ model attribute name" (for example, _ name) and be case-insensitive)

/// </Summary>

/// <Param name = "model"> model object to be assigned a value </param>

/// <Param name = "p"> an attribute </param>

/// <Param name = "parentcontrol"> Control container (such as the page or master station control) </param>

Private Static void setcontrolvaluetomodel (object model, propertyinfo P, control parentcontrol)

{

Control control = parentcontrol. findcontrol ("_" + P. Name );

If (control! = NULL)

{

Type T_c = control. GetType ();

Switch (t_c.fullname)

{

Case "system. Web. UI. webcontrols. textbox": setvalue (model, P, (textbox) control). Text); break;

Case "system. Web. UI. webcontrols. checkbox": setvalue (model, P, (checkbox) control). Checked); break;

Case "system. Web. UI. webcontrols. checkboxlist ":

Checkboxlist CBl = (checkboxlist) control );

Stringbuilder sb = new stringbuilder ();

For (INT I = 0; I <CBl. Items. Count; I ++)

{

If (CBL. items [I]. Selected)

{

SB. append (I );

SB. append (",");

}

}

Setvalue (model, P, SB. tostring (). trimend (','); break;

Case "system. Web. UI. webcontrols. Image": setvalue (model, P, (image) control). imageurl); break;

Case "system. Web. UI. webcontrols. dropdownlist": setvalue (model, P, (dropdownlist) control). selectedvalue); break;

Case "system. Web. UI. webcontrols. radiobuttonlist": setvalue (model, P, (radiobuttonlist) control). selectedvalue); break;

Case "system. Web. UI. webcontrols. hiddenfield": setvalue (model, P, (hiddenfield) control). Value); break;

Default: break;

}

}

}

 

/// <Summary>

/// Assign the value to the specified attribute of the specified model object

/// </Summary>

/// <Param name = "model"> model object to be assigned a value </param>

/// <Param name = "p"> an attribute </param>

/// <Param name = "value"> value of the attribute to be assigned to the Model </param>

Private Static void setvalue (object model, propertyinfo P, object value)

{

If (P. propertytype. fullname = "system. guid ")

{

P. setvalue (model, new GUID (value. tostring (), null );

}

Else

{

P. setvalue (model, convert. changetype (value, P. propertytype), null );

}

}

 

# Endregion

 

# Region reflection model binding Page Control

 

/// <Summary>

/// Bind the model value to the corresponding control on the page (the control must be named by "_ model attribute name" (for example, _ name) and be case-insensitive)

/// </Summary>

/// <Param name = "model"> assign a value to the Model </param>

/// <Param name = "parentcontrol"> Control container (such as the page or master station control) </param>

Public static void bindcontrols (object model, control parentcontrol)

{

Type T = model. GetType ();

Propertyinfo [] Pi = T. getproperties ();

Foreach (propertyinfo P in PI)

{

Setmodelvaluetocontrol (model, P, parentcontrol );

}

}

 

/// <Summary>

/// Assign the model value to the control on the page (currently only for Web)

/// </Summary>

/// <Param name = "model"> assign a value to the Model </param>

/// <Param name = "p"> an attribute of the Model </param>

/// <Param name = "parentcontrol"> Control container (such as the page or master station control) </param>

Private Static void setmodelvaluetocontrol (object model, propertyinfo P, control parentcontrol)

{

Control control = parentcontrol. findcontrol ("_" + P. Name );

If (control! = NULL)

{

Type T_c = control. GetType ();

Switch (t_c.fullname)

{
Case "system. Web. UI. webcontrols. textbox": (textbox) control). Text = P. getvalue (model, null). tostring (); break;
Case "system. Web. UI. webcontrols. Label": (Label) control). Text = P. getvalue (model, null). tostring (); break;

Case "system. Web. UI. webcontrols. Literal": (literal) control). Text = P. getvalue (model, null). tostring (); break;

Case "system. Web. UI. webcontrols. Image": (image) control). imageurl = P. getvalue (model, null). tostring (); break;

Case "system. Web. UI. webcontrols. dropdownlist": (dropdownlist) control). selectedvalue = P. getvalue (model, null). tostring (); break;

Case "system. Web. UI. webcontrols. radiobuttonlist": (radiobuttonlist) control). selectedvalue = P. getvalue (model, null). tostring (); break;

Case "system. Web. UI. webcontrols. checkbox": (checkbox) control). Checked = (bool) p. getvalue (model, null); break;

Case "system. Web. UI. webcontrols. checkboxlist ":

String [] arr = (string) p. getvalue (model, null). Split (',');

Checkboxlist CBl = (checkboxlist) control );

For (INT I = 0; I <arr. length; I ++)

{

CBl. items [Int. parse (ARR [I])]. Selected = true;

}

Break;

Case "system. Web. UI. webcontrols. hiddenfield": (hiddenfield) control). value = P. getvalue (model, null). tostring (); break;

Default: break;

}

}

}

# Endregion

 

}< br> public class person
{< br> Public string name {set; get ;}< br> Public String sex {set; get ;}
}< BR >}

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.