Eksport fra gridview til Excel - der sker bare keine!
Hej!Jeg har et gridview med en knap (Button1_Click) og den tilhørende kode ses nedenfor. Når jeg debugger melder den ikke fejl, men når jeg trykker på knappen, sker der intet. Der eksporteres ingen fil. Hvorfor?
--- start kode aspx.cs filen ---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class hr_kompetence_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtFind_TextChanged(object sender, EventArgs e)
{
//GridView1.
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.FilterParameters.Clear();
SqlDataSource1.FilterExpression = "Initials LIKE '{0}' OR [First Name] LIKE '{1}' OR [Last Name] LIKE '{2}' OR [Competence Code] LIKE '{3}' OR [Description] LIKE '{4}'";
SqlDataSource1.FilterParameters.Add("Initials", "*" + txtFind.Text + "*");
SqlDataSource1.FilterParameters.Add("[First Name]", "*" + txtFind.Text + "*");
SqlDataSource1.FilterParameters.Add("[Last Name]", "*" + txtFind.Text + "*");
SqlDataSource1.FilterParameters.Add("[Competence Code]", "*" + txtFind.Text + "*");
SqlDataSource1.FilterParameters.Add("[Description]", "*" + txtFind.Text + "*");
}
private void Button2_Click(object sender,
System.EventArgs e)
{
ExportToExcel("Report.xls", GridView1);
}
private void ExportToExcel(string p, GridView GridView1)
{
throw new NotImplementedException();
}
private void ExportToExcel(string strFileName, DataGrid dg)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition",
"attachment;filename=" + strFileName);
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
--- stop kode aspx.cs filen ---