?
Solved

ASP.NET, obout suite: ExportToExcel from Grid

Posted on 2009-02-18
20
Medium Priority
?
3,135 Views
Last Modified: 2013-11-26
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
Comment
Question by:Cerixus
  • 11
  • 9
20 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676117
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23676175
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676326
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676341
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23676497
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23676526
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676556
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23676591
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676600
Are you getting a javascript error?
0
 
LVL 1

Author Comment

by:Cerixus
ID: 23676649
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676658
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23676717
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676776
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23677354
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23677418
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23677479
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
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 2000 total points
ID: 23677585
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
 
LVL 1

Author Comment

by:Cerixus
ID: 23677634
Wow.  lol... it's just that simple!
0
 
LVL 1

Author Closing Comment

by:Cerixus
ID: 31548512
Amazing!  Thanks for sticking this one out!  :)
0
 
LVL 1

Author Comment

by:Cerixus
ID: 23785668
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

809 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