Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当网格位于ContentPlaceHolder中时,将网格导出到asp.net中的excel_Asp.net_Gridview_Export - Fatal编程技术网

当网格位于ContentPlaceHolder中时,将网格导出到asp.net中的excel

当网格位于ContentPlaceHolder中时,将网格导出到asp.net中的excel,asp.net,gridview,export,Asp.net,Gridview,Export,我正在尝试将gridview数据导出到excel。该网格位于ContentPlaceHolder中。那页有母版页。。当我按下“导出到excel”按钮时,其给出的错误为“控件“GridView”类型的“ContentPlaceholder 1\u GridView1”必须放置在带有runat=server的表单标记中。但是当我在ContentPlaceHolder中添加表单标记时。。它的给定错误为“一个页面只能有一个服务器端表单标记。”。在我的主页上有一个表单标签。在这种情况下,如何导出网格数据。

我正在尝试将gridview数据导出到excel。该网格位于ContentPlaceHolder中。那页有母版页。。当我按下“导出到excel”按钮时,其给出的错误为“控件“GridView”类型的“ContentPlaceholder 1\u GridView1”必须放置在带有runat=server的表单标记中。但是当我在ContentPlaceHolder中添加表单标记时。。它的给定错误为“一个页面只能有一个服务器端表单标记。”。在我的主页上有一个表单标签。在这种情况下,如何导出网格数据。。。感谢Cs代码:---

`受保护的void btnexOptexcel_Click(对象发送方,事件参数e)

….
---->我尝试添加了

public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
`在cs页面。并删除了ContentPlaceHolder标记中的“asp:form”标记。但是,当我在调试模式下运行应用程序时,会出现错误,因为“RegisterForEventValidation只能在Render()期间调用;

------->我尝试将代码修改为`

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
         <table>
             <tr id="trGV" runat="server">
             <td>
              <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="true" ></asp:GridView>
             </td>
            </tr>
          <tr>
          <td align="right" >           
                <asp:Button ID="btnExoprtExcel" runat="server" Text="Export to Excel" 
                    onclick="btnExoprtExcel_Click" />
          </td>
        </tr>       
         </table>
    </asp:Content>

`通过删除表单标记和updatepanel。但仍然在调试点给出错误,因为“RegisterForEventValidation只能在Render()期间调用;”任何一个都可以建议其他解决方案


通过在中使用“EnableEventValidation=“false”从母版页中删除表单标记,并将其放置在内容页中,可以解决此问题

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" runat="server">
// your html markup
</form></asp:Content>

//您的html标记
您只能将一个表单标记放置在母版页或内容页中。但如果你把它放在内容页上,你必须对每个内容页都这样做 编辑:RegisterForEventValidation只能在Render()期间调用;要解决此错误,必须将EnableEventValidation=“false”添加到@Page指令到母版页指令

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportGridviewtoExcel.aspx.cs" Inherits="ExportGridviewtoExcel" EnableEventValidation="false"%>

在您的.cs页面中 添加此命名空间

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



protected void btnExoprtExcel_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GV1.AllowPaging = false;
    //Change the Header Row back to white color
    GV1.HeaderRow.Style.Add("background-color", "#000000");
    //Applying stlye to gridview header cells
    for (int i = 0; i < GV1.HeaderRow.Cells.Count; i++)
    {
        GV1.HeaderRow.Cells[i].Style.Add("background-color", "#000000");
    }
    int j = 1;
    //This loop is used to apply stlye to cells based on particular row
    foreach (GridViewRow gvrow in GV1.Rows)
    {

        if (j <= GV1.Rows.Count)
        {
            if (j % 2 != 0)
            {
                for (int k = 0; k < gvrow.Cells.Count; k++)
                {
                    gvrow.Cells[k].Style.Add("background-color", "#FFFFFF");
                }
            }
        }
        j++;
    }
    GV1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
使用System.IO;
公共覆盖无效VerifyRenderingInServerForm(控件)
{
/*确认已为指定的ASP.NET呈现HtmlForm控件
运行时的服务器控件*/
}
受保护的void btnexOptexcel_Click(对象发送方,事件参数e)
{
Response.ClearContent();
Response.Buffer=true;
AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,“Details.xls”);
Response.ContentType=“应用程序/ms excel”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
GV1.AllowPaging=false;
//将标题行更改回白色
GV1.HeaderRow.Style.Add(“背景色”,“000000”);
//将stlye应用于gridview标题单元格
对于(int i=0;iif(j)首先感谢您的回答。我尝试了您的解决方案,但我的母版页中有一个ScriptManager。在执行代码时,其给出的错误为“Control'ScriptManager'类型的'ScriptManager'必须放在runat=server的表单标记中。”这个问题还有其他解决方案吗..谢谢在母版页中保留表单标记并从内容页中删除更新面板我在没有更新面板的情况下尝试了它,它工作了在我尝试之前不要关闭表单标记,但仍然给出错误,因为“RegisterForEventValidation只能在呈现()期间调用”您能推荐其他解决方案吗?Thank我通过删除“ContentPlaceHolder”中的标记尝试了这一点。但在调试阶段给出的错误是“RegisterForEventValidation只能在Render()期间调用”;您是否需要
?删除
,并且
,它将正常工作
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportGridviewtoExcel.aspx.cs" Inherits="ExportGridviewtoExcel" EnableEventValidation="false"%>
   using System.IO;
   public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}



protected void btnExoprtExcel_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GV1.AllowPaging = false;
    //Change the Header Row back to white color
    GV1.HeaderRow.Style.Add("background-color", "#000000");
    //Applying stlye to gridview header cells
    for (int i = 0; i < GV1.HeaderRow.Cells.Count; i++)
    {
        GV1.HeaderRow.Cells[i].Style.Add("background-color", "#000000");
    }
    int j = 1;
    //This loop is used to apply stlye to cells based on particular row
    foreach (GridViewRow gvrow in GV1.Rows)
    {

        if (j <= GV1.Rows.Count)
        {
            if (j % 2 != 0)
            {
                for (int k = 0; k < gvrow.Cells.Count; k++)
                {
                    gvrow.Cells[k].Style.Add("background-color", "#FFFFFF");
                }
            }
        }
        j++;
    }
    GV1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
Try the given below code. This is an simplest example for exporting gridview data to word and excel format. Thank you.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    string str = WebConfigurationManager.ConnectionStrings["work2ConnectionString1"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            GridView1.Visible = false;
            SqlConnection con = new SqlConnection(str);
            string com = "Select * from details";
            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "sName";
            DropDownList1.DataValueField = "sId";
            DropDownList1.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);

        SqlCommand cmd = new SqlCommand("select * from details where sId = '" + DropDownList1.SelectedValue + "'", con);
        SqlDataAdapter Adpt = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        Adpt.Fill(ds, "details");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        GridView1.Visible = true;
    }
    private void ExportGrid(string fileName, string contentType)
    {
       // throw new NotImplementedException();

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;fileName=" + fileName);
        Response.Charset = "";
        Response.ContentType = contentType;

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.Close();
        Response.End();

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        ExportGrid("GridviewData.xls", "application/vnd.ms-excel");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ExportGrid("GridViewData.doc", "application/vnd.ms-word");
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}