Advertisement

06.13.2008 at 08:47AM PDT, ID: 23483127
[x]
Attachment Details

Exporting GridView to Excel - usual stuff isn't working

Asked by SeTech in Microsoft Visual C#.Net, Programming for ASP.NET, Visual Studio .NET 2005

Tags: ASP.Net

I know that these days, the question of exporting a GridView to Excel is a tired, worn-out one. Questions and answers and websites aboutn on this topic, so don't think I'm just asking the same old how-do-I here. I've got a problem that's annoying, but likely has a simple solution, I just don't know what it is.

So the setup is simple. A GridView. An ImageButton that is supposed to export to Excel when clicked. It's not the only GridView-to-Excel setup I've worked with, and the others are functioning properly. My suspicion is that what's throwing this one off is the fact of having the Edit/Delete column. The last column in the GridView is an Edit/Delete column that is a TemplateColumn. And when I try to export to Excel, the error message I get says "Control 'ctl07' of type 'DataControlLinkButton' must be placed inside a form tag with runat=server.". And if I try to override the "VerifyRenderingInServerForm(Control control)" method, I get yet another error about not being able to call it when I'm trying to, blah blah blah. I even tryied to remove the last column before exporting (as you'll see inside the code snippet below), but it doesn't seem to be working.

In order to try to resolve earlier issues, I am implementing a method I got from the website http://mattberseth.com/, that basically takes the contents of a GridView, breaks it down row by row, cell by cell, and makes everything plain text inside an HTML table, and exports THAT to Excel. But when I try to throw some kind of catch in there for the "DataControlLinkButton" control, I can't because Intellisense can't figure it out and the compiler doesn't recognize it. I even added an else in this structure that if the control inside a cell is none of the recognized one, it's supposed to just flat-out remove it. But somehow, this is slipping through, and I am officially stumped.

So I am attaching the declaration of the GridView column below, so you can see what I mean there, and also the code for my Exporting to Excel methods, so if the answer is in there, glaringly obvious, please help me see it.

Thanks in advance for all help offered!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
<asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false" />
                        <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton runat="server" Text="Update" CommandName="Update" CausesValidation="true" />
                        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkAdd" runat="server" Text="Add" Width="80%" CommandName="Insert"
                            CausesValidation="true" ValidationGroup="OkToValidate" OnClick="lnkAdd_Click" />
                    </FooterTemplate>
                </asp:TemplateField>
///////////////////////////////////////////////////////
protected void btnExport_Click(object sender, ImageClickEventArgs e)
    {
        gvResult.AllowPaging = false;
        gvResult.ShowFooter = false;
        DataControlField dt;
        dt = gvResult.Columns[7];
        gvResult.Columns.Remove(gvResult.Columns[7]);
        Export("claimdir.xls", gvResult);
        gvResult.AllowPaging = true;
        gvResult.ShowFooter = true;
        gvResult.Columns.Insert(7, dt);
    }
 
    /// <summary>
    /// Got this online from http://mattberseth.com/
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    public static void Export(string fileName, GridView gv)
    {
        string style = @"<style> .mytext { mso-number-format:\@; } </style> ";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
 
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a table to contain the grid
                Table table = new Table();
 
                //  include the gridline settings
                table.GridLines = gv.GridLines;
 
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
 
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
 
                //  add the footer row to the table -- not in this page
                //if (gv.FooterRow != null)
                //{
                //    PrepareControlForExport(gv.FooterRow);
                //    table.Rows.Add(gv.FooterRow);
                //}
 
                //  render the table into the htmlwriter
                table.RenderControl(htw);
 
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
 
    /// <summary>
    /// Replace any of the contained controls with literals
    /// Got this online from http://mattberseth.com/
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            else if (current is TextBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as TextBox).Text));
            }
            else
            {
                control.Controls.Remove(current);
            }
 
            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }
[+][-]06.13.2008 at 09:12AM PDT, ID: 21780347

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 10:12AM PDT, ID: 21780859

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 10:48AM PDT, ID: 21781176

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 11:03AM PDT, ID: 21781311

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 11:14AM PDT, ID: 21781408

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 11:27AM PDT, ID: 21781512

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 11:33AM PDT, ID: 21781563

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 12:00PM PDT, ID: 21781794

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 12:21PM PDT, ID: 21781978

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.13.2008 at 12:31PM PDT, ID: 21782058

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 12:42PM PDT, ID: 21782145

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 12:58PM PDT, ID: 21782289

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 01:05PM PDT, ID: 21782350

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 01:12PM PDT, ID: 21782394

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 01:28PM PDT, ID: 21782514

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 01:49PM PDT, ID: 21782663

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 04:58AM PDT, ID: 21792664

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 06:10AM PDT, ID: 21793196

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 06:42AM PDT, ID: 21793462

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 07:03AM PDT, ID: 21793676

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 07:17AM PDT, ID: 21793813

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 07:32AM PDT, ID: 21793971

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Programming for ASP.NET, Visual Studio .NET 2005
Tags: ASP.Net
Sign Up Now!
Solution Provided By: raterus
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.16.2008 at 07:59AM PDT, ID: 21794222

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 08:06AM PDT, ID: 21794303

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 08:15AM PDT, ID: 21794422

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.16.2008 at 08:18AM PDT, ID: 21794443

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628