mehul9595
mehul9595

Reputation: 1955

Change the url of asp.net page from code behind

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

Upvotes: 4

Views: 29387

Answers (4)

Lloyd
Lloyd

Reputation: 29668

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

Upvotes: 3

user338195
user338195

Reputation:

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.

Upvotes: 2

Guffa
Guffa

Reputation: 700402

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.

Upvotes: 6

anishMarokey
anishMarokey

Reputation: 11397

it will be a new request .

You can try with Response.Redirect or Server.Transfer

Upvotes: 1

Related Questions