【问题标题】:Export gridview to any format将gridview导出为任何格式
【发布时间】:2014-04-28 19:05:41
【问题描述】:

我正在尝试将 gridview 导出为 excel/pdf/word 格式

我已经添加了所有必要的实现,但它仍然给我一个错误,比如“'gridview' 类型的控件必须放在带有 runat=server 的表单标签内”

请看我下面的代码,

.aspx 页面

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/OrganizationMaster.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="SearchInvoice.aspx.cs" Inherits="Site.pages.ContactStaff.ViewInvoice" %>

.aspx.cs 页面

  public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

.ascx.cs 控件

 protected void btnExport_Click(object sender, EventArgs e)
    {
        if (ddlExport.SelectedIndex != 0)
        {
              if (ddlExport.SelectedValue == "Word")
            {
                Response.AddHeader("content-disposition", "attachment;filename=OtherCourses.doc");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = "application/vnd.word";

                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                List<InvoiceHelper> invoicelist = svc.RetriveInvoiceList(Request.Url);
                grdinvoicelist.AllowPaging = false;
                grdinvoicelist.DataSource = invoicelist;
                grdinvoicelist.DataBind();
                grdinvoicelist.RenderControl(htmlWrite);

                Response.Write(stringWrite.ToString());
                Response.End();
            }
        }
    }

【问题讨论】:

  • 设计页面呢?
  • 你在谈论 .ascx 页面吗?
  • .aspx 因为您也使用了母版页。 标签可能有任何冲突。

标签: c# export-to-excel


【解决方案1】:

你可能和我有同样的问题。

在您的 MasterPage 中,您已经有一个表单标签, 当您在网络表单中执行第二个表单标记时。

尽量使用最干净的母版页,例如:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="TotalEmpty.master.cs" 

Inherits="ProjectName.MasterPages.TotalEmpty" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

【讨论】:

  • 尝试在控件内仅使用一个表单标签。 (不在母版页或调用控件的网页中)
猜你喜欢
  • 2017-10-22
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多