Use QueryString to initiate page, use form POST after

Posted on 2009-12-26
Last Modified: 2012-06-21
Hello Experts:

Using 2.0...  Making transition from classic ASP to

I've got an existing classic ASP page that uses form GET/QueryString.  It works great as there are only a few criteria that the page really needs to be able to change the view of a dynamic page that shows database reports.  I am porting this page over to and am struggling with the best way to use the new features of (and follow "best practices"), while maintaining some of the nice features of the existing classic ASP page.

One of the nice things about the classic ASP page, after a user selected the criteria they wanted for their report (and got the "view" they wanted, they could easily copy the URL and share it or post it somewhere (all the criteria for the report was in the URL).  With, and all the "pre-wired" stuff, there is a bunch of extra stuff that shows up in the query string, and it certainly isn't as nice to share.  So I think I would like to use POST with this new page.  My thought was, I could provide a link (with QueryString) somewhere on the page, and tell the user if they want to share this particular "report view" with someone else, they can copy the provided link and use that.  My idea was, the page would first check the querystring for "page initialization" to set the initial criteria, then use POST after that to exchange information between postbacks.

Problem:  Once someone navigates to the page using this query string, the query string doesn't go away after the first postback.  Two problems with this...  I can't figure out whether I'm supposed to be pulling my criteria from the query string to "initialize" or if I should be using the POST form data (I suppose I could with IsPostback).  More importantly, the URL and QueryString doesn't change, but the actual criteria being used for the report DOES change with each postback, and that isn't reflected in the URL, so confusion could easily arise from this.

Maybe I am trying to solve this problem the wrong way and there is a better way to do it.  If you know of a better way, let me know.  But I suppose to solve my immediate problem with the way I'm trying to do it, I would need to know how to clear the querystring as shown in the user's address bar on a post back.
Question by:Jahelka
    LVL 11

    Accepted Solution

    1. I don't think so that ASP.NET have any problems with the QueryString and there are no strange/extra stuff that appears with QueryString. Although, QueryString object of ASP.NET provides extra features but the concept remains the same. I am guessing that in your classic ASP page, you used Response.Redirect on form-submission for showing the page again with updated criteria if you allowed the user to interact with any of page controls. You can follow the same in ASP.NET if that makes you easy.
    2. It looks that whatever your page does using initial QueryString values, it's the default behavior but due to some user-interaction with the page controls (textboxes, buttons, etc) you wish to show to new page-contents/report but still wish to have it accessible in future purpose by presenting them with a URL. Considering the following for both these requirements:
    You design your page to look for a Session variable (Boolean type) that will tell you if the user is accessing this page directly from the URL (GET) or through postback (POST). If the session variable is empty or not set, try parsing the URL for QueryString and process the request, otherwise, just process the request as it has been postbacked using IsPostBack but creat a possible URL with the QueryString values that could be used by the end user to call the page with the updated values and display that URL on top of the page in a LABEL / TEXTBOX / HYPERLINK. Further, facilitate the user to access the page without any QueryString values as a default behavior.
    And if you wish to still clear the QueryString, I don't see any other solution that (1) either use Response.Redirect; (2) Use some JavaScript embedding to change the location of the page (but this may create a request to the server similar to Response.Redirect so 1 is better). I hope this helps.
    LVL 1

    Author Comment

    The strange/extra stuff that appears in the query string would be the ViewState stuff (very long).  I just did a little more digging in to what the ViewState is and how it works.  If I could turn off ViewState and get a nice short QueryString back, that would be the ideal solution.

    I'm not currently using response.redirect, as with my classic ASP page, my QueryString is nice and short so I have always been using GET and no POST.

    It is starting to sound to me like I need to investigate what I need to do to "manually" maintain state.  I'm currently using a couple of hidden fields that are initially set on a "start up screen" if the desired data is not contained in the QueryString.  Sometimes I use links and Javascript to change those hidden field values.  Other parts of the criteria are set via pull down boxes.  So if I were to disable ViewState, am I correct to assume that I would then need to set the selected property for the correct item in the pull down box manually (along with my hidden field values) on page load like I currently do in my classic ASP?
    LVL 11

    Assisted Solution

    by:Muhammad Ousama Ghazali
    Sorry to reply you back late.
    1. I could not understand completely earlier that you are making use of GET method even for FORM SUBMISSION. However, I'll recommend you to avoid this and try using the POST method and let your users copy a URL generated by your code during postbacks which could be displayed in an attention inviting manner on top of the page.
    2. As I wrote earlier and above, write your code in CodeBehind so that it takes care of both the GET (URL only) method as well as POST (FORM submission) method.
    3. Normally, there is no need to manually maintain/disable the ViewState if you try to code as I have mentioned above, but otherwise, yes you have to set the correct selected item in the drop down lists, etc. by yourself.
    LVL 1

    Author Comment

    Hey mog,

    Sorry for the delay getting back.  I have had my head buried in dynamically creating gridviews with template fields AND dynamically adding hyperlinks (was trying to do linkbuttons first) to those template fields based on data in the row.  Spent several days on that.  Could have done the same thing in classic ASP in like 10 minutes.  I feel like I still have a LOT to learn.

    I think I have decided on using POST form submission and letting the ViewState do it's thing.  But I still want to provide a link that will allow someone to return to that exact view (using QueryString).  Again, no problem there, I can just look for that and set the necessary variables on load to make that happen.  The original problem in my original question still exists.  How do I get rid of that QueryString after I have processed it?  I don't see how response.redirect would help me in this situation...  If the form needs some variables initialized, how can I do that via response.redirect without using a QueryString (that I'm trying to get rid of)?

    I'm starting to think that IF there is QueryString data in the URL, I could use response.redirect to send that QueryString to a helper aspx page, which would take that query string data and place it in a form with hidden fields, then find a way to submit that form data in the helper aspx page back to the original page, which would give me a URL free of QueryString data.  Is something like this possible?

    LVL 1

    Author Comment

    Something just popped into my head.  I don't ever use session variables...  But maybe process the query string, set session variables, then response.redirect would do the trick...
    LVL 1

    Author Closing Comment

    This solution got me pointed in the right direction.  My final solution:  If querystring data was available, I put those variables into the session state, then redirected back to page, where I processed the session variables to initialize the form.  Not ideal, but it got the job done.  Thanks mog.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction HyperText Transfer Protocol ( or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
    A Change in PHP Behavior with Session Write Short Circuit ( (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

    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

    17 Experts available now in Live!

    Get 1:1 Help Now