Asp.net dynamically loads user-defined controls and converts them to HTML code
Source: Internet
Author: User
If the page is simply created using JS, a large number CodeAnd is not intuitive.
In Asp.net, we can create user-defined controls and return the HTML code of user-defined controls through Ajax requests.
Copy codeThe Code is as follows: 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 the textbox txtbox1 = ctrl. findcontrol ("textbox1") as textbox; // obtain the control whose ID is "textbox1"
Txtbox1.text = "test"; // initialize string result for the control;
Try
{
CTRL. rendercontrol (htmlwriter );
}
Catch {}
Finally
{
Htmlwriter. Flush ();
Result = build. tostring ();
}
Return result; // return the HTML code of the control.
}
Htmlwriter. Flush ();
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