在C#中,RenderControl是一个方法,用于将Web控件的内容呈现到指定的HtmlTextWriter对象中。
以下是RenderControl方法的使用示例:
StringWriter stringWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
Button btn = new Button();
btn.Text = "Click me";
btn.RenderControl(writer);
string renderedHtml = stringWriter.ToString();
完整示例:
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
Button btn = new Button();
btn.Text = "Click me";
btn.RenderControl(writer);
string renderedHtml = stringWriter.ToString();
// 输出呈现的内容
Response.Write(renderedHtml);
}
}
}
当访问页面时,将输出一个按钮的HTML代码:
<input type="submit" name="ctl00$MainContent$btn" value="Click me" id="MainContent_btn" />