Asp.net dynamically loads user defined controls and converts them to HTML code
- 2020-05-09 18:24:17
- OfStack
If the page is simply created using js, it takes a lot of code and is not intuitive.
In asp.net, we can actually create a user-defined control and return the user-defined control HTML code via an Ajax request.
In asp.net, we can actually create a user-defined control and return the user-defined control HTML code 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");// Loads the user-defined control TextBox txtBox1 = ctrl.FindControl("TextBox1") as TextBox;// To obtain id As a" TextBox1 "Control
txtBox1.Text = " test "; // Initialize the control string result;
try
{
ctrl.RenderControl(htmlWriter);
}
catch { }
finally
{
htmlWriter.Flush();
result=build.ToString();
}
return result;// Return control HTML code
}
htmlWriter.Flush();