Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Beginning ASP.NET Web Pages with WebMatrix
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 13th, 2014, 12:35 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 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
Reply With Quote
  #2 (permalink)  
Old May 13th, 2014, 04: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();
});
Reply With Quote
The Following User Says Thank You to Mike Brind For This Useful Post:
jnweststrate (May 13th, 2014)
  #3 (permalink)  
Old May 13th, 2014, 06: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
Reply With Quote
  #4 (permalink)  
Old May 13th, 2014, 10: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 11:09 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 06:26 AM
Exporting to Excel swhite Access VBA 2 September 18th, 2003 10:42 AM
Exporting to Excel JJ Access 1 June 9th, 2003 12:30 PM



All times are GMT -4. The time now is 04:13 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.