Currently I have a page that when you fill out a text box and click a button, it redirects you to another page.
The page needs to be loaded, since it updates and shows xml. (I cannot currently change how this is)
However what I what to do is after page was redirected once, redirect it again or just load another page in general.
The thing to note about the xml link, is that part of it is created with the text box, so it will be dynamic.
I currently have something along the lines of this
//please note that username is a textbox, I've just left it out
<script runat = "server">
void Button_Click(Object sender, EventArgs e)
{
var url = "http://website.com/scripts/" + username.text "/value/0"
try
{
Response.Redirect(url, true);
}
catch(Exception ex)
{//From what I learnt, adding true to redirect throws an exception,
//which is how I tried executing another redirect, but it doesn't seem to
//to load the first direct, and skips straight to this, I also put this
//in finally, because it seemed more appropriate to no avail
Response.Redirect(someurl, true);
}
}
So I'm wondering if this is actually possible, I also wonder if I'm just looking up the wrong keywords to find a solution.
I've spent a bit of time on this, and have yet to come to some sort of solution, but I'm new to web development so I may just be missing some incredibly simple.
Also I only really understand how C# works in asp, but am willing to learn how to add in javascript or VB if necessary.
Thanks in advance for the help
Edit: Solution!
So I managed to use javascript to append the textbox value to the xml link, request it and without showing the user (showing the user, is not necessary in this case).
After which a popup confirms that it is successful then reloads the page.
it is very self explanatory but what I did was
url = "website";
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, true);
window.alert("success");
return true;//this reloads the page, that or just window.location.reload();
For an added check, I will see if I can verify that the username is a valid username, and popup with failure text if not.
You seem to have a misunderstanding about what Response.Redirect(...)
actually does. The method name is, in my opinion, a bit misleading. It suggests that somehow the Response
to the currently executing request will be sent somewhere else than the requesting browser. This is not the case. The name could as well have been Response.SendRedirectResponseToBrowser
, because that's what Response.Redirect
does.
So when you do Response.Redirect(url)
you are telling the server that is executing your page that is should send a response to the browser, telling the browser to do a GET
request of the supplied url
. The browser will then do that, at which point that page needs to include a separate Redirect
in order to further tell the browser where to go next.
In this case then, the page at "http://website.com/scripts/" + username.text "/value/0"
needs to be patched up so that after processing the request, it will also send a redirect response with the url you want to display next.
If you have no control over that page, then you must solve this some other way. Some options:
ajax
to request the "http://website.com/scripts/" + username.text "/value/0"
url. Then after completion set the page location to the url you want to show next.http://website.com/....
url in a _blank
target, then set to location to the next page.System.Net.Http.HttpClient
in your code behind method to request the http://website.com/....
url, then do a redirect. This means that the server requests the url as part of processing the button click.Notes:
http://website.com/....
url updates some state (like store some changes in a database or similar), then you should request it using a POST
request, not a GET
. GET
requests can get a cached response which means that the server might never actually see the request, and therefore not do any processing. "http://website.com/scripts/" + username.text "/value/0"
looks risky. You should at the very minimum url encode the username.text
- HttpUtility.UrlEncode(username.text)
. Better yet would be the first validate that the entered username is actually a valid user name.You can add a Refresh header ( not a meta-refresh element) to the response that contains the XML. In the header, you can specify another URL and the number of seconds to wait before redirecting.
I guess it should be using JavaScript (front-end) instead of back-end error handling, because it goes to another page. Use promise to handle exception
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.