VerifyRenderingInServerForm:找不到合适的方法来覆盖C#错误

时间:2015-04-23 08:04:09

标签: asp.net datagridview

我正在编写一个代码来导出excel gridview。我正在使用updatepanel,因此整页都不会加载。所以,我写了一个代码:

aspx.cs代码

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代码

<asp:Button ID="btnExport" runat="server" Text="  Export  " OnClick="Export_click" />
<Triggers>
        <asp:PostBackTrigger ControlID="btnExport" />
</Triggers>

但是在这里,我收到了VerifyRenderingInServerForm(Control control)函数中的错误。错误是:

VerifyRenderingInServerForm(System.Web.UI.Control)': no suitable method found to override

如何解决错误?

3 个答案:

答案 0 :(得分:3)

我得到了答案。删除VerifyRenderingInServerForm(Control control)功能&amp;使用

System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
            Controls.Add(form);
            form.Controls.Add(gv);
            form.RenderControl(htmlWrite);

所以,我们的最终代码是

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();

答案 1 :(得分:1)

评论时,该课程为MasterPage而不是Page,这就是没有方法可以覆盖VerifyRenderingInServerForm的原因。 MasterPage继承自UserControl,行为与此类似。

因此您必须将此代码移至页面。如果不可能,您应该使用事件驱动的通信来触发master的导出功能。

  • 将事件添加到母版页。
  • 只要母版页需要与其内容页面进行通信,就会引发事件。在这种情况下,在按钮单击事件处理程序
  • 在需要执行某些操作的内容页面中创建事件处理程序。在这种情况下,导出功能

http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/interacting-with-the-content-page-from-the-master-page-cs

答案 2 :(得分:0)

谢谢!

我一直在寻找解决方案。我之前查看了你的代码,我似乎无法让它工作 - 但这是因为缺乏知识。所以对于可能会遇到同样问题的其他人来说,这是我的方案和解决方案,请注意这是在VB中,可以使用任何在线转换工具进行转换:

场景:我有一个带有导出按钮的母版页,该按钮使用母版页从页面上的gridview导出数据。

在使用Gridview的页面的Page_Load事件中,我使用了以下代码:

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

然后我创建了公共子:

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

然后添加以下内容:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub

不幸的是,这意味着它需要进入您希望从中导出gridview的每个页面,但它为我完成了这项工作。这只会导出gridview中的数据。

我希望这可以帮助那些遇到同样问题的人。