Solved

Export Gridview to Excel in VB.net using a masterpage

Posted on 2013-12-13
12
1,066 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now