Avatar of Howard Bash
Howard Bash
Flag 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).
Microsoft SharePointC#.NET Programming

Avatar of undefined
Last Comment
Randy Downs

8/22/2022 - Mon
Howard Bash

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.
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.
Howard Bash

ASKER
"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#.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Randy Downs

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
Randy Downs

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.