?
Solved

Export Gridview to Excel in VB.net using a masterpage

Posted on 2013-12-13
12
Medium Priority
?
1,177 Views
Last Modified: 2013-12-22
I have a gridview that I am trying to export to a excel spreadsheet. i take all my code and past it into a page without a master page and it works fine. I take the same code and past it into a page that has a master page and it doesn't work. I don't get any errors but it never opens excel or indicates that it is downloading the file. It just runs the code and nothing happens. Does anyone have a solution for this?

Thanks
0
Comment
Question by:whiwex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
12 Comments
 

Accepted Solution

by:
whiwex earned 0 total points
ID: 39717815
Here is my code
Protected Sub lbtnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnExport.Click
       
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=SponsorsExport.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Try

            Using sw As New StringWriter()
                Dim hw As New HtmlTextWriter(sw)

                gvSponsors.RenderControl(hw)
                Dim style As String = "<style> .textmode { } </style>"
                Response.Write(style)
                Response.Output.Write(sw.ToString())
                Response.Flush()
                Response.Close()
            End Using
        Catch ex As Exception

        End Try
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39718794
Where is the button declared?  Is the only difference in your code when it worked, and now, that you added a master page?
0
 

Author Comment

by:whiwex
ID: 39718887
Yes that is the only diffference
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:whiwex
ID: 39718888
This is the code that works
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Create Sponsor Excel Spreadsheet Works.aspx.vb" Inherits="Chairman_Sponsor_Program_Create_Sponsor_Excel_Spreadsheet" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            border: 1px solid #000000;
            background-color: #C0C0C0;
        }
    </style>
</head>
<body>
 <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

    <form id="form1" runat="server">
    <div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Button ID="dbtnInfo" runat="server" Style="display: none" />
                 <cc1:ModalPopupExtender ID="dbtnInfo_ModalPopupExtender" runat="server"
                     BackgroundCssClass="modalBackground" DynamicServicePath="" Enabled="True"
                     PopupControlID="pnlInfo" TargetControlID="dbtnInfo">
                 </cc1:ModalPopupExtender>
       
    <table class="style1">
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label2" runat="server" CssClass="Labels"
                    Text="Create Sponsor's Excel Spreadsheet "></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: left">
                <asp:LinkButton ID="lbtnExport" runat="server" CssClass="LabelsSmall">Export To Excel</asp:LinkButton>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label3" runat="server" CssClass="Labels"
                    Text="Select School Year:"></asp:Label>
                <asp:DropDownList ID="ddlSchoolYear" runat="server" CssClass="LabelsSmall" AutoPostBack="true">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label4" runat="server" CssClass="Labels"
                    Text="Select Spreadsheet Type:"></asp:Label>
                <asp:DropDownList ID="ddlType" runat="server" CssClass="LabelsSmall" AutoPostBack="true">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: Left">
                <asp:Label ID="lblTotSponsors" runat="server" CssClass="LabelsSmall"></asp:Label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="lblTotDonations" runat="server" CssClass="LabelsSmall"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:GridView ID="gvSponsors" runat="server" BackColor="#DEBA84"
                    BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
                    CellSpacing="2" Font-Names="Verdana" Font-Size="9px">
                    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
    </table>
    <asp:Panel ID="pnlInfo" runat="server" CssClass="modalpopup"
                     style="display:none" Width="450px">
                     <asp:UpdatePanel ID="UpdatePanel6" runat="server">
                         <ContentTemplate>
                             <table style="width: 100%">
                                 <tr>
                                     <td align="center">
                                         <asp:Label ID="lblInfo" runat="server" CssClass="ModalPopupHeadingLabel"></asp:Label>
                                     </td>
                                 </tr>
                                 <tr>
                                     <td align="center"
                                         style="font-size: small; font-weight: normal; font-style: normal; font-family: Arial, Helvetica, sans-serif">
                                         <asp:UpdateProgress ID="UpdateProgress6" runat="server"
                                             AssociatedUpdatePanelID="UpdatePanel6">
                                             <progresstemplate>
                                                 <asp:Image ID="UpPro6" runat="server" Height="20px"
                                                     ImageUrl="~/Images/Red Circle Transparent Background.gif" Width="20px" />
                                                 One moment please...
                                             </progresstemplate>
                                         </asp:UpdateProgress>
                                     </td>
                                 </tr>
                                 <tr>
                                     <td align="center">
                                         <asp:Button ID="btnInfoOk" runat="server" CausesValidation="False"
                                             CssClass="Buttons" Text="Ok" />
                                     </td>
                                 </tr>
                             </table>
                         </ContentTemplate>
                     </asp:UpdatePanel>
                 </asp:Panel>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
0
 

Author Comment

by:whiwex
ID: 39718890
This is the code that doesn't work because it's in a master page
<%@ Page Title="Export Sponsors To Excel" Language="VB" EnableEventValidation = "false" MasterPageFile="~/Master Pages/Sponsor Program.master" AutoEventWireup="false" CodeFile="Create Sponsor Excel Spreadsheet.aspx.vb" Inherits="Chairman_Sponsor_Program_Reports_Create_Sponsor_Excel_Spreadsheet" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style1
        {
            width: 100%;
            border: 1px solid #000000;
            background-color: #C0C0C0;
        }
    </style>
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Button ID="dbtnInfo" runat="server" Style="display: none" />
                 <cc1:ModalPopupExtender ID="dbtnInfo_ModalPopupExtender" runat="server"
                     BackgroundCssClass="modalBackground" DynamicServicePath="" Enabled="True"
                     PopupControlID="pnlInfo" TargetControlID="dbtnInfo">
                 </cc1:ModalPopupExtender>
    <table class="style1">
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label2" runat="server" CssClass="Labels"
                    Text="Create Sponsor's Excel Spreadsheet "></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: left">
                 <asp:LinkButton ID="lbtnExport"  runat="server" CssClass="LabelsSmall">Export To Excel</asp:LinkButton>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label3" runat="server" CssClass="Labels"
                    Text="Select School Year:"></asp:Label>
                <asp:DropDownList ID="ddlSchoolYear" runat="server" CssClass="LabelsSmall" AutoPostBack="true">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label4" runat="server" CssClass="Labels"
                    Text="Select Spreadsheet Type:"></asp:Label>
                <asp:DropDownList ID="ddlType" runat="server" CssClass="LabelsSmall" AutoPostBack="true">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: Left">
                <asp:Label ID="lblTotSponsors" runat="server" CssClass="LabelsSmall"></asp:Label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="lblTotDonations" runat="server" CssClass="LabelsSmall"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:GridView ID="gvSponsors" runat="server" BackColor="#DEBA84"
                    BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
                    CellSpacing="2" Font-Names="Verdana" Font-Size="9px">
                    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                &nbsp;</td>
        </tr>
    </table>
    <asp:Panel ID="pnlInfo" runat="server" CssClass="modalpopup"
                     style="display:none" Width="450px">
                     <asp:UpdatePanel ID="UpdatePanel6" runat="server">
                         <ContentTemplate>
                             <table style="width: 100%">
                                 <tr>
                                     <td align="center">
                                         <asp:Label ID="lblInfo" runat="server" CssClass="ModalPopupHeadingLabel"></asp:Label>
                                     </td>
                                 </tr>
                                 <tr>
                                     <td align="center"
                                         style="font-size: small; font-weight: normal; font-style: normal; font-family: Arial, Helvetica, sans-serif">
                                         <asp:UpdateProgress ID="UpdateProgress6" runat="server"
                                             AssociatedUpdatePanelID="UpdatePanel6">
                                             <progresstemplate>
                                                 <asp:Image ID="UpPro6" runat="server" Height="20px"
                                                     ImageUrl="~/Images/Red Circle Transparent Background.gif" Width="20px" />
                                                 One moment please...
                                             </progresstemplate>
                                         </asp:UpdateProgress>
                                     </td>
                                 </tr>
                                 <tr>
                                     <td align="center">
                                         <asp:Button ID="btnInfoOk" runat="server" CausesValidation="False"
                                             CssClass="Buttons" Text="Ok" />
                                     </td>
                                 </tr>
                             </table>
                         </ContentTemplate>
                     </asp:UpdatePanel>
                 </asp:Panel>
    </ContentTemplate>
    </asp:UpdatePanel>
   
</asp:Content>
0
 

Author Comment

by:whiwex
ID: 39718897
Here is the button code behind that i use for both pages

 Protected Sub lbtnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnExport.Click
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=SponsorsExport.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Using sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)

            gvSponsors.RenderControl(hw)
            'style to format numbers to string
            Dim style As String = "<style> .textmode { } </style>"
            Response.Write(style)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.[End]()
        End Using
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39719082
When you click the button, is there a post-back?
0
 

Author Comment

by:whiwex
ID: 39719173
Yes there is a post back
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 39719400
Where is the ScriptManager declared in the page using marter page?
Your page (that does not work) does not have ScriptManager like the original page has. You may be missing it.
0
 

Author Comment

by:whiwex
ID: 39719411
The script manager is in the master page.
0
 

Author Comment

by:whiwex
ID: 39724123
I had to point it to a new blank page using navaguateurl
0
 

Author Closing Comment

by:whiwex
ID: 39734438
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question