Export Gridview to Excel in VB.net using a masterpage

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
whiwexAsked:
Who is Participating?
 
whiwexConnect With a Mentor Author Commented:
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
 
Bob LearnedCommented:
Where is the button declared?  Is the only difference in your code when it worked, and now, that you added a master page?
0
 
whiwexAuthor Commented:
Yes that is the only diffference
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
whiwexAuthor Commented:
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
 
whiwexAuthor Commented:
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
 
whiwexAuthor Commented:
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
 
Bob LearnedCommented:
When you click the button, is there a post-back?
0
 
whiwexAuthor Commented:
Yes there is a post back
0
 
Miguel OzSoftware EngineerCommented:
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
 
whiwexAuthor Commented:
The script manager is in the master page.
0
 
whiwexAuthor Commented:
I had to point it to a new blank page using navaguateurl
0
 
whiwexAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.