views:

40

answers:

4

Hi All,

I am trying to change change the url of current aspx page to the other url on a click of button. Using Request.Url.AbsoluteUri i can get the url but is it possible to modify?. If yes, what actions will it take i mean will it be a new request or a post back.

Please let me know your views.

Thanks, Mehul makwana

+2  A: 

You can use Request.Redirect or Server.Transfer, although I'd use the former rather than the latter.

Lloyd
+1  A: 

it will be a new request .

You can try with Response.Redirect or Server.Transfer

anishmarokey
That will cause a post back. Is it possible to do without post back?
KhanS
Thanks i'm clear now.
mehul9595
no. if you want to move to other url you need to do postback. then only all the controls initilize
anishmarokey
+1  A: 

No, it's not possible to modify the URL of the current page from code behind.

When the code runs, a new request or postback is already in progress, so the current page will not exist any more once the new response is complete.

When the page that is currently being created loads in the browser, it's URL will be used instead of the URL of the current page. This URL has already been decided before the request, so you can't change that either.

What you can do is to use the Response.Redirect method to return a redirection page to the browser with the URL that you want. The browser will then make another request to the server to get the page with that URL.

If you want to change the URL of the page to get without using a redirect, doing it in code behind is too late. You have to change what the button does using client script, so that it requests the new URL directly without doing a postback.

Guffa
heyy thats nice information from your side. Well by using client side javascript do you mean to say using window.location.href function but even that creates new request i think.
mehul9595
@mehul9595: Yes, if you want to change the URL that the browser displays in the address field, a new request is the only way.
Guffa
So making that new request would result in performance issue. do you have any other way of doing it. otherwise i will go with this one only.
mehul9595
@mehul9595: No, there is no other way. It's not possible to make the browser display anything else in the address field than the URL of the page that is loaded.
Guffa
yup ur right. Thanks buddy for helping.
mehul9595
A: 

Consider using jQuery or similar library - it will allow you to modify DOM of the page. If you can generate new URL only on a server, than consider sending an ajax request to a server. Web method will return you some custom object which will contain a new URL. You'll use data returned by a web method and replace it using a jQuery or any similar library.

vikp
hey can you give any reference link or code snippet i'm not getting what you mean to say.
mehul9595
Idea is that you click on a link. Web browser makes an AJAX call to a web method (web service) through a jQuery. Web service looks at your request, decides what to do, builds a new URL and sends it back to the function that has initially sent a request. In the function you read returned data (URL) and replace your existing URL.
vikp