views:

81

answers:

3

Is there a difference between me using Javascript to redirect to URL + "?Querystring=value" versus using whatever mechanism ASP.NET uses?

If there is a difference, how can I make the rendered ASP.NET page be submitted to the same URL with a different query string by javascript?

+1  A: 

The only potential difference is that a querystring parameter is sent via GET, a form is (usually) sent by POST.

GET has a much smaller data limit as browsers have a max URL length (it varies)

You could use javascript to do a form.submit() which shoul emulate what ASP.Net does

Basiclife
Thanks! How do I make Javascript change the target URL of the form?
MakerOfThings7
`form.action='www.example.com';`
Basiclife
You can access GET variables in ASP.Net using `Request.QueryString("ParameterName")` or more generally through `Request("ParameterName")`
Basiclife
Oh, you shoul be aware that if you handle the querystring bit yourself, you should a) use encodeURIComponent() to URL encode the variables. You'll also need to be aware that any built-in functionality will only see your QueryString GET parameter - it will be up to you to split out the various parameters
Basiclife
+3  A: 

If you want to do a post back just like a asp control like a asp:Button you can use the javascript functions included by the framework to do so:

__doPostBack('ControlIDOfEventYouWantToRaise','');

You can read more about the __doPostBack in this article:

Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net

Just doing a form.submit() will not be exactly the same as using __doPostBack.

To answer the first part of your question there is no difference doing a redirect if you are just doing a Response.Redirect as the will both do a GET. The difference is if you use a asp:Button control for instance, it will access your page first to handle the button (a post back) and then do a GET on the redirected page.

If you want to submit to the same URL (eg post your data) then you should use the __doPostBack method. If you don't require the data to be posted, then just do a redirect in javascript to the same URL with a modified query string (which will just do a basic GET) but your data will not be posted.

Kelsey
Thanks Kelsey, that should do the trick!
MakerOfThings7
A: 

I somewhat disagree with Basiclife's answer; if you have any code inside something like

if (IsPostBack) {

it's not going to be equivalent, ie the code is going to be executed if you're just setting the URL. Also, controls keep their state across postbacks but are freshly initialized if you're calling the URL again. This is due to ASP.NET trying to emulate a "normal" application, so the way to make sure a normal call and a postback have the same effect might result in "de-ASP.NET-ing" the entire page.

I'm not sure if what you want works. There probably is a way. But I heavily suspect there's a better way of doing this. If you get a postback for free, and can transmit data, why is it crucial that the data shows up in the URL, instead of being comfortably posted? I can see how you want a page to respond to a URL parameter, and how you might want to change the same parameter later on based on what's happening on that page, but since you always know you're posting back, you can eg override that URL parameter in that case, by something you're posting back. This doesn't sound so nice, but it might actually be less messy. Particularly since you seem to have a reason to not abandon the postback at all (otherwise you could just use a link, right?).

Nicolas78