即使GridView位于表单标签内,也必须将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>

后面的代码:


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也位于带有runat =“ server”的from标记内,仍然出现此错误。


有什么线索吗?


白猪掌柜的
浏览 438回答 3
3回答

catspeake

重写VerifyRenderingInServerForm的替代方法是在执行渲染时从控件集合中删除网格,然后在完成页面加载之前将其重新添加。如果您想拥有一些通用的辅助方法来获取表格html,这将很有帮助,因为您不必记住添加替代。Control parent = grid.Parent;int GridIndex = 0;if (parent != null){&nbsp; &nbsp; GridIndex = parent.Controls.IndexOf(grid);&nbsp; &nbsp; parent.Controls.Remove(grid);}grid.RenderControl(hw);if (parent != null){&nbsp; &nbsp; parent.Controls.AddAt(GridIndex, grid);}避免覆盖的另一种方法是:grid.RenderBeginTag(hw);grid.HeaderRow.RenderControl(hw);foreach (GridViewRow row in grid.Rows){&nbsp; &nbsp; row.RenderControl(hw);}grid.FooterRow.RenderControl(hw);grid.RenderEndTag(hw);
打开App,查看更多内容
随时随地看视频慕课网APP