Home  >  Article  >  Backend Development  >  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

高洛峰
高洛峰Original
2017-02-03 15:12:261126browse

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();

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!

Statement:
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