关闭

Render Control to HTML String. VerifyRenderingInServerForm Method

876人阅读 评论(1) 收藏 举报

Sometimes you can get "...must be placed inside a form tag with runat=server" error, when you deal with "asp:panel" or "asp:linkbutton" or whatever else scenarious when html form control needs to be rendered for the specified control at run time. The problem can be solved (thanks, Alex) ovverriding Page.VerifyRenderingInServerForm Method. Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.


So, the code would look like the following (rendering asp:panel containing controls itself):


private void Page_Load(object sender, EventArgs e) 
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    contentPanel.RenderControl(htmlWriter);
    string s = stringWriter.ToString();

    Response.Write(s);
    Response.End();
}


public override void VerifyRenderingInServerForm(Control control) 
{
    return;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21094次
    • 积分:264
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:18篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档