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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yordan Georgiev
    New Member
    • Nov 2006
    • 4

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

    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:
    Last edited by Yordan Georgiev; Mar 15 '08, 12:14 PM. Reason: the form did not accept all the text I copied ...
  • Yordan Georgiev
    New Member
    • Nov 2006
    • 4

    #2
    Never mind - I found out not perfect but a plausible solution:

    So, first :
    1. In the aspx page set the EnableEventVali dation = "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 VerifyRendering InServerForm ( Control control )
    {
    return;
    }

    3. write the actual export to excel method as follows:
    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)(up.F indControl ( "table_list _gv" ));
    //GridView table_list_gv = (GridView)(this .FindControlFro mUpdatePanel ( ref msg , up ));
    //START THE EXPORT
    //table_list_gv.V isible = false ;
    Response.Clear ( );
    Response.AddHea der ( "content-disposition", "attachment;fil ename=Type_Here _Your_File_Name .xls" );
    Response.Charse t = "";

    // If you want the option to open the Excel file without saving then
    // 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 );


    table_list_gv.R enderControl ( htmlWrite );

    Response.Write ( stringWrite.ToS tring ( ) );
    //table_list_gv.V isible = true;

    Response.End ( );

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


    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

    4. Hopefully did not forget something ...
    Corrections , questions - firstName.LastN ame@gmail.com

    Comment

    Working...