Reputation: 4607
I have the following code:
if (function.Equals("PopUp"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + trans_number, "http://localhost:4000/Handler.ashx");
if (result.Equals("True") || result.Equals("true"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
}
Session["result"] = result;
Response.Redirect("Results.aspx");
}
This code makes a request to a server and if the result is true, it should create a new tab as well as redirect the current window to Results.aspx.
If the result is false, it should only redirect to Results.aspx.
The main problem with this code is that the new tab is never created, even if the result is true. However, if I comment out all of the code except the new tab code, then the new tab is created.
Why is this happening? How can I correct it?
Upvotes: 0
Views: 2922
Reputation: 4796
The problem seems to be that you're redirecting before your script can execute. I would try doing the redirect in script too, so something like this;
if (function.Equals("PopUp"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + trans_number, "http://localhost:4000/Handler.ashx");
if (result.Equals("True") || result.Equals("true"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
}
Session["result"] = result;
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.location.href = 'http://localhost:4000/Redirect.aspx.aspx'", true);
}
Upvotes: 2
Reputation: 6220
There is bound to be an issue with the result not matching what you're testing with.
String.Equals()
may not always match against a given string, as sometimes if the strings have been interned the references might not match.
I would recommend switching to using String.Compare()
or better yet just using the equality operator: ==
So:
if (result == "True" || result == "true")
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
}
Or better yet:
if (Convert.ToBoolean(result))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
}
MSDN has some specific guidelines for how to effectively compare strings:
http://msdn.microsoft.com/en-gb/library/vstudio/cc165449.aspx
Upvotes: 0