Use the RenderControl method to dynamically load User Controls

Source: Internet
Author: User

Insus. NET believes that many netizens will dynamically Add User Controls. For example, the blog post written before Insus. NET:
Http://www.cnblogs.com/insus/archive/2012/04/16/2451085.html
Http://www.cnblogs.com/insus/articles/2023678.html
Or a blog post related to a user control: http://www.cnblogs.com/insus/category/341697.html


However, Insus. NET wants to use the RenderControl method of the control to dynamically load the user control.
Create a user control in the site, write some files and pull an image.

InsusUserControl. ascx

 
On the site, create a. aspx webpage and pull a control:

 


In. aspx. cs, write a private method. The Render user control is a string with a parameter that is used to input the user control file name, including its directory.

In the. aspx. cs Page_Load event, you can obtain the string after the user control Render and assign it to the Literal control.

Page_Load (= RenderUserControlToString (


To solve the exceptions that occur when the control. RenderControl () method is executed, you also need to write two methods in override on the webpage:

{View Code


After writing, run:

 

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.