Assign a value to the user control (ascx) property (property)

Source: Internet
Author: User

This blog post by Insus. NET is an experience and tips on assigning values to attributes of User Controls (ASCX.
This is the case. There are generally many categories for news sites. The latest, TOP, or most popular news or articles are displayed on the homepage of the website, which is classified by category. Therefore, we will design a user control. The data source of the user control is from the Article table, and only input the ID values and Top values of different category to implement this.

The user control. cs is about the following. The highlighted code has two attributes. Of course, your real environment may only have one or more attributes.



Now we will demonstrate how to reference user controls on a webpage and assign values to them. There are several methods or reconstruct and optimize them.
Create a webpage, that is, on your site, which page needs to reference this user control. The user control is displayed on the webpage aspx.

First, it is the simplest and most commonly used. Open. aspx and switch to design mode,



Then, pull the user control into the webpage:



Now you can switch to the source mode to assign values directly in. aspx. Refer to the highlighted attribute assignment.



Second, if you do not want to assign values in. aspx, you can assign values in. aspx. cs. After the user control is introduced into the webpage, press F7 to come to The. aspx. cs code page.




The first method and the second method have different positions in the assignment.
OK. Let's demonstrate the third method. For the purpose of achieving the best results and long-term design, Insus. NET first writes an interface. The interface subject is a function, and the return type is UserControl.



The user control implements this interface. Refer to the highlighted code:



To achieve dynamic loading, you do not have to switch between design and source on the webpage. Pull a control container in the webpage



Go to the. aspx. cs code program, refer:

 

Reference two namespaces #9 and #10 lines of code. The #9 code is used to apply the Reflection technology.
Line #22 of the code dynamically loads user controls and converts them to interfaces.
Line #25 and #31 Code. The string at the arrow position is the two Public attribute names of the user control.

 

 

Line #28 and #34 Code assign values to the two attributes.
Line #37 of the Code adds the user control to the PlaceHolder container.

Okay, there are three ways to teach you how to assign values to user control attributes on a webpage. I believe you have grown a lot in technology.
The entire demo has been completed.


Next, Insus. NET will attach another technology to you for reconstruction. Let's look at the code of the third method. 25th to 29 and 31 to 35 groups of code, the similarity is very high. Therefore, to reduce code redundancy, you need to write them as a method.



Remove comment code. Extracted and written as the SetValue () method. In this way, the Code becomes much clearer. It looks like this:

Finished or not. Is there any more?
Yes, it's not done yet. Here we can see that Insus. NET has very few attributes for each user control. There may be no relationship between a few words. If there are more words, you have to write a lot.

SetValue(uc, , , );


These similar codes. To solve this problem, Insus. NET refactored them. In the hope of better results.



In fact, you can still write the 32nd lines of code as an extension method:



That is to say, store the value assignment part in a collection.
Haha, it's done. The method can be selective. If you think it is good, you can use it.

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.