Reputation: 5157
I am writing a code to export to excel a gridview. I am using updatepanel, so whole page doesnot loads. So, i wrote a code :
aspx.cs Code
protected void Export_click(object sender, EventArgs e)
{
GridView gv = (GridView)TBMMasterContentPalceHolder.FindControl("myGridView");
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Suppliers.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gv.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control) //To prevent Export To Excel Error
{
}
asp Code
<asp:Button ID="btnExport" runat="server" Text=" Export " OnClick="Export_click" />
<Triggers>
<asp:PostBackTrigger ControlID="btnExport" />
</Triggers>
But here, i am getting the error in VerifyRenderingInServerForm(Control control)
function. Error is :
VerifyRenderingInServerForm(System.Web.UI.Control)': no suitable method found to override
How to solve the error ?
Upvotes: 2
Views: 6644
Reputation: 1911
Thank you!
I have been searching for a solution for quite sometime. I looked over your code before and I couldnt seem to get it working - But this was because of a lack of knowledge. So for anyone else that might sit with this same problem, here was my scenario and solution, please note that this is in VB and can be converted using any online converting tool :
Scenario : I have a master page with an export button which exports data from a gridview on a page using the masterpage.
Dim myMasterPage As MasterPageName = Page.Master
Dim exportButton As System.Web.UI.WebControls.Button = myMasterPage.FindControl("ButExportExcel")
If (exportButton IsNot Nothing) Then
AddHandler exportButton.Click, AddressOf Me.ButExportExcel_Click
End If
I then created the public sub:
Private Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sMethod = "ButExportExcel_Click"
Dim sErrorMessage = ""
Dim sExportFileName As String = ""
Try
sExportFileName = Path.GetFileName(Request.PhysicalPath)
sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"
Response.Clear()
Response.AddHeader("content-disposition", "attachment; filename=" & sExportFileName)
Response.ContentType = "application/vnd.xls"
Dim WriteItem As System.IO.StringWriter = New System.IO.StringWriter()
Dim htmlText As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(WriteItem)
SummaryGridView.AllowPaging = False
'Dim dtSupplier As DataTable = CType(ViewState("dtSupplier"), DataTable)
'SummaryGridView.DataSource = dtSupplier
SummaryGridView.DataBind()
SummaryGridView.RenderControl(htmlText)
Response.Write(WriteItem.ToString())
Response.End()
Catch ex As Exception
End Try
End Sub
Then adding the below :
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
This unfortunately means it needs to go onto every page that you want a gridview to be exported from, but it did the job for me. This only exports the data in the gridview.
I hope this helps anyone who is sitting with the same problem.
Upvotes: 0
Reputation: 460238
As commented the class is a MasterPage
not a Page
, that's why there is no method to override VerifyRenderingInServerForm
. A MasterPage
inherits from UserControl
and behaves like one.
So you have to move this code to the page. If that's not possible you should use event-driven communication to trigger the export functionality from master.
Upvotes: 1
Reputation: 5157
I got my answer. Remove VerifyRenderingInServerForm(Control control)
function & use
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
Controls.Add(form);
form.Controls.Add(gv);
form.RenderControl(htmlWrite);
So, our final code would be
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=myfile.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
Controls.Add(form);
form.Controls.Add(gv);
form.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Upvotes: 4