Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Beginning ASP.NET Web Pages with WebMatrix
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET Web Pages with WebMatrix
This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2014, 11:35 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Exporting The Razor WebGrid to Excel - Using Variables

Hello,

I have been working with Mike's Post regarding the Excel Export from The Razor Webmatrix Grid. The article is posted here for reference.

Everything is working exactly as expected (thanks Mike!), but I am now trying to pass variables into the iframe web page so I can restrict my Query based on a POST variable.

The jQuery code looks like this (where "excel" is the id of the img link in the referring page):
Code:
...
        $('#excel').on('click', function () {
            $('<iframe src="/GenerateExcel"></iframe>').appendTo('body').hide();
        });
...
And I'm looking for a URL like this:
...<iframe src="/GenerateExcel?USER=jsmith&TYPE=Analyst"></iframe>...

I believe the variables USER and TYPE need to be posted to jQuery, but I'm not sure how to pull them in. Here's my attempt so far, but I'm missing something:
Code:
...
        $('#excel').on('click', function () {
            var USER = tr.find('#USER').val();
            var TYPE = tr.find('#TYPE').val();
            $.post(
                  '<iframe src="/GenerateExcel"></iframe>',
                  {
                   USER: USER,
                   TYPE: TYPE
                   }
            ).appendTo('body').hide();
        });
...
Thanks for all you do!

Joel
 
Old May 13th, 2014, 03:19 PM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

Actually, all you should need to do is to add the querystring values to the url you create for the iframe:

Code:
$('#excel').on('click', function () {
    var user = tr.find('#USER').val();
    var type = tr.find('#TYPE').val();
    $('<iframe src="/GenerateExcel?user=" + user + "&amp;type=" + type + "></iframe>').appendTo('body').hide();
});
The Following User Says Thank You to Mike Brind For This Useful Post:
jnweststrate (May 13th, 2014)
 
Old May 13th, 2014, 05:36 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for the quick response Mike! Makes perfect sense.
If I use the <img> as you describe in the article referenced above, I believe i need to figure out a different way of using jQuery to pull in the URL variables.

The URL of the page where the <img> link is located is something like this:
.../WebPage.cshtml?USER=jsmith&TYPE=Analyst

so instead of
...
var user = tr.find('#USER').val();
var type = tr.find('#TYPE').val()
...

I believe I need to be using different jQuery syntax, perhaps with another function that retrieves the values from the URL. I apologize as I'm not as versed in jQuery as I should be, but I feel this is the right solution.

Thanks again for your help!
Joel
 
Old May 13th, 2014, 09:22 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2014
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Figured it out

Thanks to Mike's article "Exporting The Razor WebGrid To Excel Using OleDb" and his guidance here, I was able to find a solution.

My objective was to export an excel file based on a Webmatrix Webgrid using the methods described in the article above. However, I needed to pass a variable through a URL Querystring to help me limit the results in the Excel export to a specific user.

The URL of the origination page, as mentioned above, looks like this:
.../WebPage.cshtml?USER=jsmith&TYPE=Analyst

I used this jQuery code to pass information on to the "GenerateExcel.cshtml" file:
Code:
/* Get URL QueryString Parameters*/
(function($) {
    $.QueryString = (function(a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i)
        {
            var p=a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'))
})(jQuery);

/* Pass request on to GenerateExcel file */
$(function () {
    $('#excel').on('hover', function () {
        $(this).css('cursor', 'pointer');
    });
    $('#excel').on('click', function () {
        var user = $.QueryString["USER"];
        var type = $.QueryString["TYPE"];
        $('<iframe src="/GenerateExcel?User=' + user + '&Type=' + type + '"></iframe>').appendTo('body').hide();
    });
});
Note the use of single and double quotes in the iframe URL.
Also, the jQuery that splits the URL Querystring was found at this StackOverflow post.

Using the above jQuery in tandem with Mike's code from the article mentioned above, I am now able to export an excel file using specific parameters defined in the URL Querystring.

Regards,
Joel

Last edited by jnweststrate; May 13th, 2014 at 10:09 PM..




Similar Threads
Thread Thread Starter Forum Replies Last Post
WebMatrix Razor and ASP.NET MVC 3 Framework jpjamie BOOK: Beginning ASP.NET Web Pages with WebMatrix 2 February 25th, 2012 07:11 PM
Exporting to excel michaeltanfeliz Beginning VB 6 2 May 3rd, 2008 05:26 AM
Exporting to Excel swhite Access VBA 2 September 18th, 2003 09:42 AM
Exporting to Excel JJ Access 1 June 9th, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.