GridView控件必须放在一个表单标签内有RUNAT ="服务器"即使在GridView控件是一个表单标签内 [英] GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag

查看:293
本文介绍了GridView控件必须放在一个表单标签内有RUNAT ="服务器"即使在GridView控件是一个表单标签内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<form runat="server" id="f1">
    <div runat="server" id="d">
        grid view:
        <asp:GridView runat="server" ID="g">
        </asp:GridView>
    </div>

    <asp:TextBox runat="server" ID="t" TextMode="MultiLine" Rows="20" Columns="50"></asp:TextBox>
</form>

身后code:

Code behind:

public partial class ScriptTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        g.DataSource = new string[] { "a", "b", "c" };
        g.DataBind();

        TextWriter tw = new StringWriter();
        HtmlTextWriter h = new HtmlTextWriter(tw);    
        d.RenderControl(h);
        t.Text = tw.ToString();
    }
}

即使在GridView是在一个从具有=服务器标签,还是我收到此错误。

Even the GridView is within a from tag with runat="server", still I am getting this error.

任何线索吗?

推荐答案

正在调用 GridView.RenderControl(HtmlTextWriter的),因此页面抛出一​​个异常,一个服务器 - 控制是一个形式之外呈现。

You are calling GridView.RenderControl(htmlTextWriter), hence the page raises an exception that a Server-Control was rendered outside of a Form.

您可以避开这个execption通过重写<一href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.verifyrenderinginserverform.aspx">VerifyRenderingInServerForm

You could avoid this execption by overriding VerifyRenderingInServerForm

public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}

在这里看到 和的这里

这篇关于GridView控件必须放在一个表单标签内有RUNAT =&QUOT;服务器&QUOT;即使在GridView控件是一个表单标签内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆