How to pass attributes and methods (reflection) In User Controls ))

Source: Internet
Author: User

How do I pass attributes and methods in a user control? Because the control is executed after the page, it is often unable to obtain the value passed by the control. For example, to upload a file control, if it is made a user control, you cannot obtain the uploaded file name at the time of submission. The solution is to use the reflection mechanism to define the attributes of the user control, it is used to pass the value and define the user control method. It is used to pass the property value to the call page.

Page_load:

//Use the reflection mechanism to obtain the type of the user control. uctype = {name = "usctrlupload_ascx" fullname = "ASP. usctrlupload_ascx "} usctrlupload_ascx is the name corresponding to the user control name and is copied from the debugging.

TypeUctype = usctrlupload1.gettype ();

//Use propertyinfo to obtain the attributes in the user control.

PropertyinfoUctextname = uctype. getproperty ("Picname");//PicnameIs the property in the user control, as shown in the following figure.

//Set and obtain values in the user control.

Uctextname. setvalue (usctrlupload1,"123456",Null);//Use the propertyinfo instance method to test the assignment. This is just to test the effect of setting the value. The main thing is how to obtain the property value of the control.

Label2.text = uctextname. getvalue (usctrlupload1,Null). Tostring (); // usctrlupload1User Control ID.

//In this way, you can also obtain and set the property values in the control. However, it seems unstable, and the row does not work.

Label2.text = usctrlupload1.picname;

In the page button event:

Protected VoidButton#click (ObjectSender,EventargsE)

{

TypeUctype = usctrlupload1.gettype ();

//Use the methodinfo class to obtain the methods in the user control.

MethodinfoUcmethod = uctype. getmethod ("Button#click");//Button#clickControl.

//On this page, execute the methods in the user control.

Object[] Argumentarrray =New Object[2];

Ucmethod. Invoke (usctrlupload1, argumentarrray );//Call methods in the user control. Executed here !!.

PropertyinfoUctextname = uctype. getproperty ("Picname");

Label2.text = uctextname. getvalue (usctrlupload1,Null). Tostring ();//Obtained the information of the uploaded file name. And displayed on the page.

//In this way, you can also obtain and set the property values in the control. However, it seems unstable, and the row does not work.

// Label2.text = usctrlupload1.picname;

}

Appendix:

Attributes defined in the user control. CS file:

Public StringPicname

{

Get{ReturnLabel2.text ;}

Set{Label2.text =Value;}

}

Methods defined in the user control. CS file:

Public VoidButton#click (ObjectSender,EventargsE)

{

If(Fileupload1.hasfile)

{

Label2.text = fileupload1.postedfile. filename;

}

Note: The button event method is used here and can be modified at will. The widget does not contain a button. The button is placed on the page.

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.