[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use QueryString to initiate page, use form POST after

Posted on 2009-12-26
7
Medium Priority
?
652 Views
Last Modified: 2012-06-21
Hello Experts:

Using ASP.net 2.0...  Making transition from classic ASP to ASP.net/VB

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 ASP.net and am struggling with the best way to use the new features of ASP.net (and follow ASP.net "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 ASP.net, 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 ASP.net 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 ASP.net 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.
0
Comment
Question by:Jahelka
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 2000 total points
ID: 26127005
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.
0
 
LVL 1

Author Comment

by:Jahelka
ID: 26129743
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?
0
 
LVL 11

Assisted Solution

by:Muhammad Ousama Ghazali
Muhammad Ousama Ghazali earned 2000 total points
ID: 26161387
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Jahelka
ID: 26183351
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?

0
 
LVL 1

Author Comment

by:Jahelka
ID: 26183700
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...
0
 
LVL 1

Author Closing Comment

by:Jahelka
ID: 31670148
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 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