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:
<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:
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.
Please help if you can spot anything that is incorrect, it would be much appreciated. Regards,
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 gridView
to excel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With