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.Threadi ng.ThreadAbortE xception' occurred in mscorlib.dll
An exception of type 'System.Threadi ng.ThreadAbortE xception' occurred in mscorlib.dll but was not handled in user code
The thread 0x16f0 has exited with code 0 (0x0).
private Control FindControlRecu rsive ( Control root, string id )
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecu rsive ( c, id );
if (t != null)
{
return t;
}
}
return null;
} //eof FindControlRecu rsive
protected void FindControlFrom UpdatePanel ( System.Web.UI.U pdatePanel up , ref string msg )
{
foreach (Control i in up.Controls)
{
if (i.ID != null)
{
msg = "found " + i.ID + " ";
} //eof if
} //eof foreach
} //eof FindControlFrom UpdatePane
protected void btnExportToExce l_Click ( object sender, EventArgs e )
{
string msg = " " ;
System.Web.UI.U pdatePanel up = (System.Web.UI. UpdatePanel)(th is.FindControlR ecursive ( this.Page.Form, "updatePnl" ));
//GridView table_list_gv = (GridView)(this .FindControlFro mUpdatePanel ( up, ref msg ));
//first get the produced gridview object
msg = msg + "found : " + table_list_gv.I D.ToString ( ) + " , "; //record it
this.Master.Err or_label.Text = msg + " i HAVE IT " + table_list_gv.I D.ToString ( ); //set the text to the error label
Debugger.WriteL ine ( msg ); //debug it
//before dealing with the export of the gridveiw stop any events
table_list_gv.A llowPaging = false;
table_list_gv.A llowSorting = false;
table_list_gv.D ataBind ( );
//now start the export
Response.Clear ( );
Response.AddHea der ( "content-disposition", "attachment;fil ename=filename. xls" );
Response.Charse t = "";
// if you want the option to open the excel file without saving than
// comment out the line below
// response.cache. setcacheability (httpcacheabili ty.nocache);
Response.Conten tType = "applicatio n/vnd.xls";
System.IO.Strin gWriter stringWrite = new System.IO.Strin gWriter ( );
System.Web.UI.H tmlTextWriter htmlWrite = new HtmlTextWriter ( stringWrite );
//render the gridview
table_list_gv.R enderControl ( htmlWrite );
//turn back the properties of the gridview
table_list_gv.A llowPaging = true;
table_list_gv.A llowSorting = true;
table_list_gv.D ataBind ( );
Response.Write ( stringWrite.ToS tring ( ) );
Response.End ( );
} //eof protected void btnexporttoexce l_click ( object sender, eventargs e )
Was is the case that the Export to Excel button should be located
Here is the code:
I get the following message :
A first chance exception of type 'System.Threadi ng.ThreadAbortE xception' occurred in mscorlib.dll
An exception of type 'System.Threadi ng.ThreadAbortE xception' occurred in mscorlib.dll but was not handled in user code
The thread 0x16f0 has exited with code 0 (0x0).
private Control FindControlRecu rsive ( Control root, string id )
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecu rsive ( c, id );
if (t != null)
{
return t;
}
}
return null;
} //eof FindControlRecu rsive
protected void FindControlFrom UpdatePanel ( System.Web.UI.U pdatePanel up , ref string msg )
{
foreach (Control i in up.Controls)
{
if (i.ID != null)
{
msg = "found " + i.ID + " ";
} //eof if
} //eof foreach
} //eof FindControlFrom UpdatePane
protected void btnExportToExce l_Click ( object sender, EventArgs e )
{
string msg = " " ;
System.Web.UI.U pdatePanel up = (System.Web.UI. UpdatePanel)(th is.FindControlR ecursive ( this.Page.Form, "updatePnl" ));
//GridView table_list_gv = (GridView)(this .FindControlFro mUpdatePanel ( up, ref msg ));
//first get the produced gridview object
msg = msg + "found : " + table_list_gv.I D.ToString ( ) + " , "; //record it
this.Master.Err or_label.Text = msg + " i HAVE IT " + table_list_gv.I D.ToString ( ); //set the text to the error label
Debugger.WriteL ine ( msg ); //debug it
//before dealing with the export of the gridveiw stop any events
table_list_gv.A llowPaging = false;
table_list_gv.A llowSorting = false;
table_list_gv.D ataBind ( );
//now start the export
Response.Clear ( );
Response.AddHea der ( "content-disposition", "attachment;fil ename=filename. xls" );
Response.Charse t = "";
// if you want the option to open the excel file without saving than
// comment out the line below
// response.cache. setcacheability (httpcacheabili ty.nocache);
Response.Conten tType = "applicatio n/vnd.xls";
System.IO.Strin gWriter stringWrite = new System.IO.Strin gWriter ( );
System.Web.UI.H tmlTextWriter htmlWrite = new HtmlTextWriter ( stringWrite );
//render the gridview
table_list_gv.R enderControl ( htmlWrite );
//turn back the properties of the gridview
table_list_gv.A llowPaging = true;
table_list_gv.A llowSorting = true;
table_list_gv.D ataBind ( );
Response.Write ( stringWrite.ToS tring ( ) );
Response.End ( );
} //eof protected void btnexporttoexce l_click ( object sender, eventargs e )
Was is the case that the Export to Excel button should be located
Here is the code:
Comment