Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

SharePoint 2010 Web Part Gridview to CSV

I have written an web part that has a gridview on it that I populate and display on assorted web part pages.   All that seems to work fine.  Now I need to capture the gridview data and present it to a user on their clicking a "Load CSV" button on that web part.  

The code follows and indeed works:
   private void ExportGridToCSV()
        {            
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Employee.csv");
            Response.Charset = "";
            Response.ContentType = "application/text";
            GridEmployee.AllowPaging = false;
            GridEmployee.DataBind();

            StringBuilder columnbind = new StringBuilder();
            for (int k = 0; k < GridEmployee.Columns.Count; k++)
            {

                columnbind.Append(GridEmployee.Columns[k].HeaderText + ',');
            }

            columnbind.Append("\r\n");
            for (int i = 0; i < GridEmployee.Rows.Count; i++)
            {
                for (int k = 0; k < GridEmployee.Columns.Count; k++)
                {

                    columnbind.Append(GridEmployee.Rows[i].Cells[k].Text + ',');
                }

                columnbind.Append("\r\n");
            }
            Response.Output.Write(columnbind.ToString());
            Response.Flush();
            Response.End();

        }

Open in new window


The problem is that once the click event calls this code, the page no longer seems to be connected to the code behind and subsequent clicks don't get handled (I have a breakpoint which is never reached in the beginning of the click events).
Avatar of Howard Bash
Howard Bash
Flag of United States of America image

ASKER

Why would that be?  The function gets called when I click a button which is part of the web part, but the button subsequently doesn't fire the event nor does another button on that same web part.
Avatar of Randy Downs
That example was not the best since it was only losing its previous text not the code.

As I understand it your code works on 1st click but not after that, Anything detected in Console? Perhaps your code is looking for a status from onClick or something along those lines. Is the button even enabled after the click?

It might be helpful to post your button script.
"That example was not the best since it was only losing its previous text not the code. "  - I have no clue what you're referring to in that statement.

The button does click but the event handler code's breakpoint isn't hit.

There is no button script.  This is CodeBehind C#.
It would be helpful to see the asp code (e.g., asp:Button) . Can you move your break point to somewhere that is triggered?
ASKER CERTIFIED SOLUTION
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial