• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3221
  • Last Modified:

ASP.NET, obout suite: ExportToExcel from Grid

Again, I'm using the obout suite of components (brilliant stuff).  I have a grid populated from a database.  In my aspx file I created a button that looks like this:

      <asp:Button ID="Button1" runat="server" Text="Export to Excel" />

In the codebehind (.aspx.vb) I have the following line in the Button1_click event:

      Grid1.ExportToExcel("Servers", False, False, True, True)

It DOES save the excel file to the folder specified in the FolderExports property, however, it does not prompt the browser to download the file.  The site is running from a nonstandard port, ie http://servername.domain.com:8910 so I'm wondering if that would have anything to do with it... I e-mailed obout support and they tried to help, but don't speak the best English.  He indicated I may be using the serverside export method instead of the clientside method, but I'm really not sure the difference between implementation for the two...

I'm using Visual Studio.
0
Cerixus
Asked:
Cerixus
  • 11
  • 9
1 Solution
 
aibusinesssolutionsCommented:
The server side method would save the file to a folder on the server.  The client side method would save the file to the Response Output Stream.

If you are stuck using the server side method, you could always save the file, and then response.redirect to the file, that would cause the users browser to download it.
0
 
CerixusAuthor Commented:
Well apparently I'm using the serverside method.  How do I implement the clientside method?    This is the page that describes the method, and testing the example does as expected (prompts for file save):  http://www.obout.com/grid/grid_exporting.aspx

All it says is "Call the exportToExcel or exportToWord client-side methods to export the data from the Grid to MS Excel or MS Word files."  Which I thought was exactly what I am doing.
0
 
aibusinesssolutionsCommented:
Yes, the client side method is javascript.  View the source of that example, this is the function you need to call:
function exportToExcel() {
		    var sFileName = document.getElementById("txtFileName").value;
		    var bKeepFormatting = document.getElementById("chkKeepFormatting").checked;
		    var bUsePaging = document.getElementById("chkUsePaging").checked;
		    var bAppendTimeStamp = document.getElementById("chkAppendTimeStamp").checked;
		    var bOffice2007 = document.getElementById("chkOffice2007").checked;
		    var bHiddenColumns = document.getElementById("chkHiddenColumns").checked;
		    
		    var sColumnsForExport = null;
		    if(document.getElementById("chkSpecifyColumns").checked == true) {
		        sColumnsForExport = getColumnsForExport();
		    }
		    
		    grid1.exportToExcel(sFileName, bKeepFormatting, bUsePaging, bAppendTimeStamp, bOffice2007, bHiddenColumns, sColumnsForExport)
		}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
aibusinesssolutionsCommented:
Then your export to excel button would be

<input type="button" class="tdText" value="Export to Excel" onclick="exportToExcel()" />

Or if you are using an asp:Button

<asp:Button runat="server" OnClientClick="exportToExcel()" />
0
 
CerixusAuthor Commented:
Okay, this is a stupid question... I've yet to use any javascript in a .net application.  Where do I put it?  Do I need to enclose your function in a <script> tag?
0
 
CerixusAuthor Commented:
Okay, here is my button:

      <asp:Button ID="Button1" runat="server" Text="Export to Excel" OnClientClick="exportToExcel()" />

and here is the javascript function, which I placed immediately before the <cc1:grid> opening tag:

     <script type="text/javascript">
         function exportToExcel() {
             Grid1.ExportToExcel("Servers", False, False, True, True)
             }
     </script>  

It just refreshes the page.  Now it doesn't even write the file.
0
 
aibusinesssolutionsCommented:
The <script> tag should go in the <head> area of the page.

You may need to put OnClientClick="exportToExcel(); return false;"

adding the return false will prevent it from doing a postback.
0
 
CerixusAuthor Commented:
hmmm, to complicate things further, I'm using a MasterPage.  I put the javascript in the <head> of the Master page, and added the "return false;" so it no longer does the postback, but still no file download.
0
 
aibusinesssolutionsCommented:
Are you getting a javascript error?
0
 
CerixusAuthor Commented:
And my mistake, it DOES still do a postback.  I also just changed the onclick event to jibberish and it responds the exact same way.  It must be something to do with the masterpage...   Googling it now but not having a whole lot of luck.
0
 
aibusinesssolutionsCommented:
Ok, just remove that asp:button then, and use a normal <input> button

<input type="button" class="tdText" value="Export to Excel" onclick="exportToExcel()" />
0
 
CerixusAuthor Commented:
Clicking on that button does nothing at all.  :(

I was able to use the attached example, found at http://www.dotnetcurry.com/ShowArticle.aspx?ID=274 and after playing with it, I can pretty much say with all confidence it's not the javascript, it's something with the "Grid1.ExportToExcel("Servers", false, false, true, true, false, false)" function itself.  It just does nothing.
 <asp:Panel ID="panelContent" GroupingText="ContentPage Controls" runat="server">
        <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
        <asp:Button ID="btnContent" runat="server" Text="Button" OnClientClick="Populate();" />
    </asp:Panel>
    <script type="text/javascript" language="javascript">
        function Populate() {
            {
                document.getElementById('<%=txtContent.ClientID%>').value = "Hi";               
            }
        }
    </script>

Open in new window

0
 
aibusinesssolutionsCommented:
If you are using a masterpage, and a content place holder, then it might not be able to see Grid1.  Also in javascript variables are case sensitive, so make sure it's not grid1 with a lowercase g.

Try something like this:

<script type="text/javascript">
  function exportToExcel() {
     myGrid = document.getElementById("ctl00_ContentPlaceHolder1_Grid1");
     myGrid.ExportToExcel("Servers", False, False, True, True)
  }
</script>   

Open in new window

0
 
CerixusAuthor Commented:
I'm assuming "ctl00_ContentPlaceHolder1_Grid1" would need to be changed to match my App?

Attached is the entire code for this ASPX file
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/MISD.Master" CodeBehind="servers-list.aspx.vb" Inherits="MISD.servers_list" 
    title="MISD Portal > List Servers" %>
 
<%@ Register Assembly="obout_Flyout2_NET" Namespace="OboutInc.Flyout2" TagPrefix="cc2" %>
<%@ Register assembly="obout_Grid_NET" namespace="Obout.Grid" tagprefix="cc1" %>
<%@ Register assembly="obout_EasyMenu_Pro" namespace="OboutInc.EasyMenu_Pro" tagprefix="oem" %>
 
 
<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
 
 
    <cc1:Grid ID="Grid1" runat="server" DataSourceID="dsserverslist" 
    FolderStyle="styles/premiere_blue" AllowAddingRecords="False" 
        AllowFiltering="True" AllowGrouping="True" AutoGenerateColumns="False" 
        PageSize="50" PageSizeOptions="25,50,100,500" AllowRecordSelection="false">
        <ScrollingSettings ScrollHeight="400" />
        <Columns>
            <cc1:Column ConvertEmptyStringToNull="False" DataField="ServerName" ID="ServerColumn" 
                HeaderText="Server Name" Width="150" ReadOnly="True" AllowGroupBy="False" templateid="servername">
            </cc1:Column>
            <cc1:Column ConvertEmptyStringToNull="False" DataField="Manufacturer" 
                HeaderText="Manufacturer">
            </cc1:Column>
            <cc1:Column ConvertEmptyStringToNull="False" DataField="Model" 
                HeaderText="Model" Width="225">
            </cc1:Column>
            <cc1:Column ConvertEmptyStringToNull="False" DataField="Serial" 
                HeaderText="Serial" Width="115" AllowGroupBy="False">
            </cc1:Column>
            <cc1:Column ConvertEmptyStringToNull="False" DataField="ExportTime" 
                DataFormatString="{0:MMMM dd, yyyy hh:mm tt}" 
                DataFormatString_GroupHeader="{0:MMMM dd, yyyy hh:mm tt}" HeaderText="Export Date" 
                Width="225" AllowGroupBy="False">
            </cc1:Column>
        </Columns>
        <Templates>
                <cc1:GridTemplate runat="server" ID="servername">
                        <Template>
                                <a href="servers-view.aspx?servername=<%# Container.Value %>"><%# Container.Value %></a>
                        </Template>
                </cc1:GridTemplate>
        </Templates>        
</cc1:Grid>
    
<input type="button" value="Export to Excel" onclick="exportToExcel()" />
<script type="text/javascript" language="javascript">
  function exportToExcel() {
     myGrid = document.getElementById("ctl00_ContentPlaceHolder1_Grid1");
     myGrid.ExportToExcel("Servers", False, False, True, True)
  }
</script> 
         
<asp:SqlDataSource>
!- Data Source information removed -!
</asp:SqlDataSource>
         
</asp:Content>

Open in new window

0
 
aibusinesssolutionsCommented:
Yeah, run your app, then view the source of the page.  Look for ID="Grid1", it should be something like "ctl00_Main_Grid1"

If so, use
myGrid = document.getElementById("ctl00_Main_Grid1");
myGrid.ExportToExcel("Servers", False, False, True, True)

0
 
CerixusAuthor Commented:
I see "<div id="ctl00_Main_Grid1_ob_Grid1MainContainer"", but I also see lots of variants like HeaderContainer, etc...

I tried ctl00_Main_Grid1_ob_Grid1MainContainer as well as just ctl00_Main_Grid1, still no luck.
0
 
aibusinesssolutionsCommented:
Ok, I just got the control and tested it out, in a masterpage, you don't need to specify the ID like that.  Here is my code, it's working for me.

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

   <cc1:Grid ID="Grid1" runat="server" DataSourceID="SqlDataSource1">
   </cc1:Grid>

   <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ca_ConnectionString %>"
        SelectCommand="SELECT [filename], [filetype], [filesize], [expiration] FROM [ca_Files]">
   </asp:SqlDataSource>
   
   <input id="Button1" type="button" value="button" onclick="Grid1.exportToExcel('Servers', false, true, false, true);" />
</asp:Content>
0
 
CerixusAuthor Commented:
Wow.  lol... it's just that simple!
0
 
CerixusAuthor Commented:
Amazing!  Thanks for sticking this one out!  :)
0
 
CerixusAuthor Commented:
For some reason it doesn't work when published to the production site... here is the follow up question, please help!  

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24194168.html
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now