ASP. NET dynamically loads user-defined controls and translates them into HTML code

Source: Internet
Author: User
If the page is simple to use JS to create, to write a lot of code, and not intuitive.
In ASP. NET, we can actually create a user-defined control that returns the HTML code of the user's custom control via an AJAX request.

public static string Rangeruscontrol (String controlname) {StringBuilder build = new StringBuilder (); HtmlTextWriter htmlwriter = new HtmlTextWriter (new StringWriter (build)); UserControl UC = new UserControl (); Control Ctrl=uc. LoadControl (controlname+ ". ascx");//load user-defined control TextBox TxtBox1 = Ctrl. FindControl ("TextBox1") as textbox;//gets the control with ID "TextBox1" txtbox1.text = "test"; Initializes a string result to the control; try {Ctrl. RenderControl (HTMLWriter); } catch {} finally {Htmlwriter.flush (); Result=build. ToString (); Return result;//returns the HTML code of the control} Htmlwriter.flush ();
Related Article

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.