By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,421 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Postback, XML/XSLT, Gridview, Response object question

P: n/a

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(e xcelXsltFileName),
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.

Jun 9 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.