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 开发者_Python百科let me know your views.
Thanks, Mehul makwana
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.
You can use Response.Redirect()
or Server.Transfer()
, although I'd use the former rather than the latter.
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.
it will be a new request
.
You can try with Response.Redirect
or Server.Transfer
精彩评论