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

exporting GridView to from UpdatePanel of a page having one master to Excel file

P: 4
I have a search form using master page , which is dynamically generated from the column names of the database - when the users have a search result it should be exported to Excel by clicking the button ...
I get the following message :
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
The thread 0x16f0 has exited with code 0 (0x0).


private Control FindControlRecursive ( Control root, string id )
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive ( c, id );
if (t != null)
{
return t;
}
}

return null;
} //eof FindControlRecursive



protected void FindControlFromUpdatePanel ( System.Web.UI.UpdatePanel up , ref string msg )
{

foreach (Control i in up.Controls)
{
if (i.ID != null)
{
msg = "found " + i.ID + " ";
} //eof if
} //eof foreach

} //eof FindControlFromUpdatePane




protected void btnExportToExcel_Click ( object sender, EventArgs e )
{
string msg = " " ;
System.Web.UI.UpdatePanel up = (System.Web.UI.UpdatePanel)(this.FindControlRecurs ive ( this.Page.Form, "updatePnl" ));
//GridView table_list_gv = (GridView)(this.FindControlFromUpdatePanel ( up, ref msg ));

//first get the produced gridview object
msg = msg + "found : " + table_list_gv.ID.ToString ( ) + " , "; //record it
this.Master.Error_label.Text = msg + " i HAVE IT " + table_list_gv.ID.ToString ( ); //set the text to the error label
Debugger.WriteLine ( msg ); //debug it

//before dealing with the export of the gridveiw stop any events
table_list_gv.AllowPaging = false;
table_list_gv.AllowSorting = false;
table_list_gv.DataBind ( );

//now start the export
Response.Clear ( );
Response.AddHeader ( "content-disposition", "attachment;filename=filename.xls" );
Response.Charset = "";
// if you want the option to open the excel file without saving than
// comment out the line below
// response.cache.setcacheability(httpcacheability.no cache);

Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter ( );
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter ( stringWrite );

//render the gridview
table_list_gv.RenderControl ( htmlWrite );

//turn back the properties of the gridview
table_list_gv.AllowPaging = true;
table_list_gv.AllowSorting = true;
table_list_gv.DataBind ( );

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


} //eof protected void btnexporttoexcel_click ( object sender, eventargs e )

Was is the case that the Export to Excel button should be located
Here is the code:
Mar 15 '08 #1
Share this Question
Share on Google+
1 Reply


P: 4
Never mind - I found out not perfect but a plausible solution:

So, first :
1. In the aspx page set the EnableEventValidation = "false" directive - I do not trust Microsoft's buggy input checking anyway - ( I have implemented my own ...
2. put the following method in the Page class :
/// <summary>
/// Confirms that an HtmlForm control is rendered for the specified ASP.NET
/// server control at run time.
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm ( Control control )
{
return;
}

3. write the actual export to excel method as follows:
protected void btnExportToExcel_Click ( object sender, EventArgs e )
{
string msg = " ";
System.Web.UI.UpdatePanel up = (System.Web.UI.UpdatePanel)(this.FindControlRecurs ive ( this.Page.Form, "updatePnl" ));
GridView table_list_gv = (GridView)(up.FindControl ( "table_list_gv" ));
//GridView table_list_gv = (GridView)(this.FindControlFromUpdatePanel ( ref msg , up ));
//START THE EXPORT
//table_list_gv.Visible = false ;
Response.Clear ( );
Response.AddHeader ( "content-disposition", "attachment;filename=Type_Here_Your_File_Name. xls" );
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.No Cache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter ( );
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter ( stringWrite );


table_list_gv.RenderControl ( htmlWrite );

Response.Write ( stringWrite.ToString ( ) );
//table_list_gv.Visible = true;

Response.End ( );

} //eof protected void btnexporttoexcel_click ( object sender, eventargs e )


private Control FindControlRecursive ( Control root, string id )
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive ( c, id );
if (t != null)
{
return t;
}
}

return null;
} //eof FindControlRecursive

4. Hopefully did not forget something ...
Corrections , questions - firstName.LastName@gmail.com
Mar 15 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.