asp.net dynamically loads user-defined controls and translates them into HTML code

Source: Internet
Author: User
Tags flush

Ajax is now quite popular technology, Ajax is not only to send messages to the server side, but more importantly, there is no reload of the page.

If the page simply use JS to create, to write a large number of code, and not intuitive.

In asp.net, we can actually create user-defined controls that return user-defined HTML code through AJAX requests.

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");//加载用户定义控件

             TextBox txtBox1 = ctrl.FindControl("TextBox1") as TextBox;//获得id为“TextBox1”的控件
             txtBox1.Text = "测试"; //给控件初始化
             string result;
             try 
             {
                 ctrl.RenderControl(htmlWriter);
             }
             catch { }
             finally
             {
                 htmlWriter.Flush();
                 result=build.ToString();
             }
             return result;//返回控件的HTML代码
         }

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.