Postback, XML/XSLT, Gridview, Response object question

R

ronc85

My environment is ASP.NET 2.0, C# and AJAX.

I'm having a problem with 'Button Click' logic which is trying to
display a Gridview and
then use XML/XSLT to build an Excel file. The following code does not
display the Gridview but opens the created file in Excel.If I comment
out the ExportDatasetToExcel routine the Gridview displays correctly.
I'm assuming the problem is something to do with the Response
methods.



protected void Button1_Click(object sender, EventArgs e)
{
ProductLocation objProductLocation = new ProductLocation ();
DataSet ds;

ds = objProductLocation.GetProductLocationList();

GridView1.DataSource = ds;
GridView1.DataBind();


string excelXsltFileName = "ProductLocationList.xsl";
string excelOutputFileName = "ProductLocationList.xls";
Hashtable excelXsltArguments = null;




ExcelUtility.ExportDatasetToExcel(Server.MapPath(excelXsltFileName),
excelOutputFileName,
ds,
Response,
excelXsltArguments
);

}



public static bool ExportDatasetToExcel(string XSLTFileNameWithPath,
string excelOutputFileName, DataSet ds, System.Web.HttpResponse
Response, System.Collections.Hashtable excelXsltArguments)
{

if (ds == null && ds.Tables[0].Rows.Count < 1)
return false;

Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition",
"attachment;filename=" + excelOutputFileName);

XslTransform xslt = new XslTransform();
xslt.Load(XSLTFileNameWithPath);

XsltArgumentList xsltArguments = new XsltArgumentList();


if (excelXsltArguments != null) // Check for parameters to be
passed to XSLT
{
foreach (System.Collections.DictionaryEntry de in
excelXsltArguments)
xsltArguments.AddParam(de.Key.ToString(), "",
de.Value);
}


XmlDocument xml = new XmlDocument();
xml.LoadXml(ds.GetXml());

xslt.Transform(xml.CreateNavigator(), xsltArguments,
Response.Output);


Response.Flush();
Response.Close();

return true;

}


Sal P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top