Solved

Export Gridview to Excel in VB.net using a masterpage

Posted on 2013-12-13
12
1,107 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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 35

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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