<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标记内,仍然出现此错误。有什么线索吗?
3 回答
catspeake
TA贡献1111条经验 获得超0个赞
重写VerifyRenderingInServerForm的替代方法是在执行渲染时从控件集合中删除网格,然后在完成页面加载之前将其重新添加。如果您想拥有一些通用的辅助方法来获取表格html,这将很有帮助,因为您不必记住添加替代。
Control parent = grid.Parent;
int GridIndex = 0;
if (parent != null)
{
GridIndex = parent.Controls.IndexOf(grid);
parent.Controls.Remove(grid);
}
grid.RenderControl(hw);
if (parent != null)
{
parent.Controls.AddAt(GridIndex, grid);
}
避免覆盖的另一种方法是:
grid.RenderBeginTag(hw);
grid.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in grid.Rows)
{
row.RenderControl(hw);
}
grid.FooterRow.RenderControl(hw);
grid.RenderEndTag(hw);
- 3 回答
- 0 关注
- 437 浏览
添加回答
举报
0/150
提交
取消