C# 'GridView' 必须放置在带有 runat=server 的表单标签内。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17305359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'GridView' must be placed inside a form tag with runat=server.
提问by Paul T. Rykiel
So, I have researched your site and my situation is unique. I have a Web control .ascx, it has a gridview on it and the code looks like this:
所以,我研究了你的网站,我的情况是独一无二的。我有一个 Web 控件 .ascx,它上面有一个 gridview,代码如下所示:
<body>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10">
<Columns>
<asp:BoundField DataField="fb_login" HeaderText="User Id" runat="server" />
<asp:BoundField DataField="fb_url" HeaderText="URL___" />
<asp:BoundField DataField="fb_response" HeaderText="Answer: Did you find what you were looking for?" />
<asp:BoundField DataField="fb_noResponse" HeaderText="No Response or Ignore" />
<asp:BoundField DataField="fb_date" HeaderText="Date" />
<asp:BoundField DataField="fb_serviceCall" HeaderText="Prevented Service Call" />
<asp:BoundField DataField="fb_partsShipment" HeaderText="Prevented Parts Shipment" />
<asp:BoundField DataField="fb_warranty" HeaderText="Under Warranty" />
<asp:BoundField DataField="fb_cancel" HeaderText="Cancelled" />
<asp:BoundField DataField="fb_none" HeaderText="None of the Above" />
</Columns>
</asp:GridView>
<asp:Button ID="download" Text=" Download to Excel " OnClick="dwnLoad" runat="server" />
</div>
I have a download to Excel button that executes the following code:
我有一个执行以下代码的下载到 Excel 按钮:
protected void dwnLoad(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=kbNotification.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWriter);
Response.End();
}
When I press the button, I am geting the following error:
当我按下按钮时,出现以下错误:
Exception Details: System.Web.HttpException: Control 'pagecontent_0_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Source Error:
Line 54: Response.Cache.SetCacheability(HttpCacheability.NoCache);
Line 55: Response.ContentType = "application/vnd.xls";
Line 56: System.IO.StringWriter stringWrite = new System.IO.StringWriter();
Line 57: System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
Line 58: GridView1.RenderControl(htmlWriter);
Source File: C:\Projects\MEAU\trunk\Code\MEAU.Web\Components\SupportCenter\KB_Notification_rpt.ascx.cs Line: 56
I have tried to add the following method to the codebehind:
我尝试将以下方法添加到代码隐藏中:
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
this does not work because this is an .ascx page, so I have also tried to add it to my default.aspx page... and am still gettng errors that it cannot find the method of overrid.
这是行不通的,因为这是一个 .ascx 页面,所以我也尝试将它添加到我的 default.aspx 页面......但仍然出现无法找到覆盖方法的错误。
Please help if you can spot anything that is incorrect, it would be much appreciated. Regards,
如果您能发现任何不正确的地方,请帮助,将不胜感激。问候,
采纳答案by KanimozhiEthiraj
protected void Page_Load(object sender, EventArgs e)
{
btnExcel_Click +=................
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
gvdetails.DataSourceID = "dsdetails";
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
BindGridview();
//Change the Header Row back to white color
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Use this code to download data from gridViewto excel.
使用此代码将数据下载gridView到 excel。
回答by Henk Holterman
A WebControl (ascx) should not contain a <body>tag.
WebControl (ascx) 不应包含<body>标记。
It produces an HTML fragment, and it should be placed on (inside) an HTML page and inside the asp:Formelement.
它生成一个 HTML 片段,它应该放在(内部)HTML 页面和asp:Form元素内部。
回答by GirishBabuC
Follow two steps
遵循两个步骤
Step 1
第1步
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Step 2
第2步
EnableEventValidation="false"

