Home > Backend Development > C#.Net Tutorial > Asp.net dynamically loads user-defined controls and converts them into HTML code

Asp.net dynamically loads user-defined controls and converts them into HTML code

高洛峰
Release: 2017-02-03 15:12:26
Original
1194 people have browsed it

If the page is simply created using js, a lot of code needs to be written, and it is not intuitive.
In asp.net, we can actually create user-defined controls and return user-defined control 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();
Copy after login

For more articles about Asp.net dynamically loading user-defined controls and converting them into HTML code, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template