Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 必须将GridView添加到表单标记以进行渲染 TextWriter tr=new StringWriter(); HtmlTextWriter=新的HtmlTextWriter(tr); HtmlForm form=新的HtmlForm(); form.Controls.Add(divQueryDescription); divQueryDescription.RenderControl(编写器);_C#_.net_Asp.net - Fatal编程技术网

C# 必须将GridView添加到表单标记以进行渲染 TextWriter tr=new StringWriter(); HtmlTextWriter=新的HtmlTextWriter(tr); HtmlForm form=新的HtmlForm(); form.Controls.Add(divQueryDescription); divQueryDescription.RenderControl(编写器);

C# 必须将GridView添加到表单标记以进行渲染 TextWriter tr=new StringWriter(); HtmlTextWriter=新的HtmlTextWriter(tr); HtmlForm form=新的HtmlForm(); form.Controls.Add(divQueryDescription); divQueryDescription.RenderControl(编写器);,c#,.net,asp.net,C#,.net,Asp.net,我的GridView在一个DIV中,我将该DIV添加到一个HtmlForm中,这样我就可以将它呈现为一个字符串。但我得到了以下错误:- Gridview“grv”必须放在runat=server的表单标记中,尽管我的Gridview已经在表单中,因为我已将DIV添加到表单中 如何将此DIV呈现为字符串?根据您在此处向我们展示的内容,GridView还没有在表单元素中使用runat=“server”,您需要执行以下操作: TextWriter tr = new StringWriter(); Ht

我的GridView在一个DIV中,我将该DIV添加到一个HtmlForm中,这样我就可以将它呈现为一个字符串。但我得到了以下错误:-

Gridview“grv”必须放在runat=server的表单标记中,尽管我的Gridview已经在表单中,因为我已将DIV添加到表单中


如何将此DIV呈现为字符串?

根据您在此处向我们展示的内容,
GridView
还没有在
表单
元素中使用
runat=“server”
,您需要执行以下操作:

TextWriter tr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(tr);
HtmlForm form = new HtmlForm();
form.Controls.Add(divQueryDescription);
divQueryDescription.RenderControl(writer);

<div runat="server" id="divQueryDescription">
    <asp:GridView runat="server" id="grv"></asp:GridView>
</div>

需要特别注意的是
部分及其结束标记

更新:


似乎
表单
正在引发问题,因为它尚未写出,您直接从
div
中写出:尝试使用
表单.RenderControl(writer)

确保表单元素/控件中有runat=“server”

只需在页面中添加以下代码即可

<form runat="server">
    <div runat="server" id="divQueryDescription">
        <asp:GridView runat="server" id="grv"></asp:GridView>
    </div>
</form>

要了解更多信息,请阅读。

请发布完整的表单设计。@Muhammad页面的html中没有表单。我正在动态创建表单,因为我需要通过电子邮件发送该内容。我已更新了我的答案,以解决我认为先前忽略的问题;但是,请不要为了引起更多的注意而发布两次您的问题-您的另一个问题中的额外数据可能会被编辑到本问题的正文中,以使其更新:@Muhammad:这将呈现完整的表单,而不是该DIV。包括表单标签在内的所有内容,viewState等将被渲染。我已将DIV动态添加到HtmlForm…请再次查看代码..GridView位于该DIV内..那么GridView不是自动添加到该表单的控件集合吗?如果我直接将GridView添加到HtmlForm的控件集合,此异常消失。在本例中,我没有要重写的此方法。因为我的DIV位于UserControl上。请在承载您的UserControl的页面中覆盖它-我没有尝试过,但这应该可以做到。您可能还需要使用page.VerifyRenderingInServerForm从UserCotrol代码调用它(此)
public override void VerifyRenderingInServerForm(Control control)
{
  return;
}