[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Export Gridview to Excel Produces Blank XLS file - Gridview Inside Update Panel...

Asked by synapse88 in Asynchronous Javascript and XML (AJAX), Programming for ASP.NET, Microsoft Visual Basic.Net

I have used this export code in a number of places with no problem, but now that the gridview is located inside an UpdatePanel, I'm getting blank excel sheets when it exports.

Where am I dropping the data?  I debugged and in the BtnExportGrid_Click subroutine, the gvProd gridview has a 0 row count, so it's dropping the data somewhere before it even gets into the GridViewExportUtil class.

Thanks for any ideas!
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:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
<asp:UpdatePanel ID="updList" runat="server">
                        <ContentTemplate>
 
<tr>
                    <td colspan="2">
                        <br />
                        <asp:Panel ID="pnlFilter" runat="server" CssClass="TextLabel" OnClick="LetterChanged">
                                <asp:LinkButton ID="lnk0" runat="server" Text="[0-9]" ></asp:LinkButton> 
                                <asp:LinkButton ID="lnkA" runat="server" Text="A" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkB" runat="server" Text="B" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkC" runat="server" Text="C" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkD" runat="server" Text="D" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkE" runat="server" Text="E" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkF" runat="server" Text="F" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkG" runat="server" Text="G" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkH" runat="server" Text="H" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkI" runat="server" Text="I" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkJ" runat="server" Text="J" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkK" runat="server" Text="K" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkL" runat="server" Text="L" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkM" runat="server" Text="M" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkN" runat="server" Text="N" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkO" runat="server" Text="O" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkP" runat="server" Text="P" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkQ" runat="server" Text="Q" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkR" runat="server" Text="R" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkS" runat="server" Text="S" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkT" runat="server" Text="T" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkU" runat="server" Text="U" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkV" runat="server" Text="V" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkW" runat="server" Text="W" AutoPostBack="True"></asp:LinkButton>                                                                                                                    
                                <asp:LinkButton ID="lnkX" runat="server" Text="X" AutoPostBack="True"></asp:LinkButton> 
                                <asp:LinkButton ID="lnkY" runat="server" Text="Y" AutoPostBack="True"></asp:LinkButton> 
                                <asp:LinkButton ID="lnkZ" runat="server" Text="Z" AutoPostBack="True"></asp:LinkButton> 
                                <asp:LinkButton ID="lnkAll" runat="server" Text="ALL" AutoPostBack="True"></asp:LinkButton>
					  <asp:Button ID="btnExportGrid" runat="server" CssClass="TextLabel" Text="Export List" ForeColor="Green" Height="22" OnClick="BtnExportGrid_Click" />
                        </asp:Panel>
 
                        <asp:Panel ID="pnlProdList" runat="server" Height="300px" Width="800px" ScrollBars="both">
                            <asp:HiddenField ID="hdnLetter" runat="server" />
                            <asp:GridView ID="gvProd" runat="server" BackColor="#EEEEEE" GridLines="horizontal" AllowSorting="false" CssClass="TextLabel" Width="780px"
                                AutoGenerateColumns="false" DataKeyNames="key" EnableViewState="false" >
                                <SelectedRowStyle BackColor="LightYellow" Font-Bold="true" />
                                <HeaderStyle BackColor="LightSlateGray" ForeColor="White" />
                                <AlternatingRowStyle BackColor="#DDDDDD" Font-Bold="false" />
                                <RowStyle Font-Bold="false" />                                
                                <Columns>                                                                        
                                    <asp:CommandField SelectText="Details." ShowSelectButton="true" />
                                    <asp:BoundField DataField="prodname" HeaderText="Name" SortExpression="prod" />
                                </Columns>                                                        
                            </asp:GridView>
                        </asp:Panel>
                       </ContentTemplate>
                        <Triggers>
                       <Triggers>
                          <asp:AsyncPostBackTrigger ControlID="lnk0" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkA" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkB" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkC" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkD" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkE" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkF" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkG" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkH" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkI" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkJ" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkK" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkL" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkM" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkN" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkO" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkP" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkQ" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkR" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkS" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkT" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkU" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkV" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkW" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkX" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkY" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkZ" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:AsyncPostBackTrigger ControlID="lnkALL" EventName="Click"></asp:AsyncPostBackTrigger>
                          <asp:PostBackTrigger ControlID="btnExportGrid" /> 
                      </Triggers>
                                </Triggers>
                    </asp:UpdatePanel>
                    </td>
                </tr>
 
 
 
    Protected Sub BtnExportGrid_Click(ByVal sender As Object, ByVal args As EventArgs)
        GridViewExportUtil.Export("ProdList.xls", Me.gvProd)
    End Sub
 
 
 
 
Imports System
Imports System.Data
Imports System.Configuration
Imports System.IO
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
 
 
Public Class GridViewExportUtil
 
    Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        Dim sw As StringWriter = New StringWriter
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        '  Create a form to contain the grid
        Dim table As Table = New Table
        table.GridLines = gv.GridLines
        '  add the header row to the table
        If (Not (gv.HeaderRow) Is Nothing) Then
            GridViewExportUtil.PrepareControlForExport(gv.HeaderRow)
            table.Rows.Add(gv.HeaderRow)
        End If
        '  add each of the data rows to the table
        For Each row As GridViewRow In gv.Rows
            GridViewExportUtil.PrepareControlForExport(row)
            table.Rows.Add(row)
        Next
        '  add the footer row to the table
        If (Not (gv.FooterRow) Is Nothing) Then
            GridViewExportUtil.PrepareControlForExport(gv.FooterRow)
            table.Rows.Add(gv.FooterRow)
        End If
        '  render the table into the htmlwriter
        table.RenderControl(htw)
        '  render the htmlwriter into the response
        HttpContext.Current.Response.Write(sw.ToString)
        HttpContext.Current.Response.End()
    End Sub
 
    ' Replace any of the contained controls with literals
    Private Shared Sub PrepareControlForExport(ByVal control As Control)
        Dim i As Integer = 0
        Do While (i < control.Controls.Count)
            Dim current As Control = control.Controls(i)
            If (TypeOf current Is LinkButton) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
            ElseIf (TypeOf current Is ImageButton) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
            ElseIf (TypeOf current Is HyperLink) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
            ElseIf (TypeOf current Is DropDownList) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
            ElseIf (TypeOf current Is CheckBox) Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
                'TODO: Warning!!!, inline IF is not supported ?
            End If
            If current.HasControls Then
                GridViewExportUtil.PrepareControlForExport(current)
            End If
            i = (i + 1)
        Loop
    End Sub
End Class
[+][-]07/13/09 01:36 PM, ID: 24843672Accepted Solution

View this solution now by starting your 30-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: Asynchronous Javascript and XML (AJAX), Programming for ASP.NET, Microsoft Visual Basic.Net
Sign Up Now!
Solution Provided By: jinal
Participating Experts: 1
Solution Grade: A
 
[+][-]08/10/09 12:15 PM, ID: 25063060Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

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

 
[+][-]08/10/09 12:21 PM, ID: 25063123Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 12:23 PM, ID: 25285240Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

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

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20090701_SELECT_ZONES