SharePoint 2010 Web Part Gridview to CSV

Howard Bash
Howard Bash used Ask the Experts™
on
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).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Howard BashSenior Software Engineer

Author

Commented:
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.
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.
Howard BashSenior Software Engineer

Author

Commented:
"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?
I don't see anything in your code about a button click. Maybe you can adapt this .

... click the Design button in the bottom of VS. Now you will see a visual representation of our page. We wish to add a Click event to the button, and this is very simply - just doubleclick the GreetButton, and you will be taken to the CodeBehind file of our page. As you can see, a fine new method has been added, called GreetButton_Click. If you have a look at the Default.aspx file (you need to go from Design view to Source view), you will see that an attribute has been added to our Button control, telling which method to call when the button is clicked. All this work done with a simple doubleclick.

Now lets add some code to our new event. We wish to use the text from the TextBox, on our good old Label with the "Hello, world!" text. This is also very simple, and all it requires is a single line of code:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial