asp.net - C# - Page Redirection not working properly -
i have following code in c#:
if (function.equals("larger50")) { request req = new request(); string result = req.dorequest("function=" + function + "&num=" + number, "http://localhost:4000/handler.ashx"); if (result.equals("true") || result.equals("true")) { page.clientscript.registerstartupscript(page.gettype(), null, "window.open('http://localhost:4000/larger.aspx?num=" + number + "', '_newtab')", true); } if(result.equals("false") || result.equals("false")) { page.clientscript.registerstartupscript(page.gettype(), null, "window.open('http://localhost:4000/smaller.aspx', '_newtab')", true); } if(result.equals("error") || result.equals("error")) { page.clientscript.registerstartupscript(page.gettype(), null, "window.open('http://localhost:4000/errorpage.htm', '_newtab')", true); } session["result"] = result; page.clientscript.registerstartupscript(page.gettype(), null, "window.location.href = 'results.aspx'", true); }
the result variable can have 1 of 3 values (if server responds):
i) true ii) false iii) error
the main problem code new tab script in each of 3 if statements work should. however, last script opens results.aspx page not executing reason or another. script written executed if other code commented out. how should solve problem?
i tried replacing response.redirect("results.aspx") exeuctes , other 3 scripts never execute.
you should register these @ once, rather in 2 separate statements:
if (function.equals("larger50")) { request req = new request(); string result = req.dorequest("function=" + function + "&num=" + number, "http://localhost:4000/handler.ashx"); string scriptval = ""; if (result.equals("true") || result.equals("true")) { scriptval = "window.open('http://localhost:4000/larger.aspx?num=" + number + "', '_newtab');"; } if(result.equals("false") || result.equals("false")) { scriptval = "window.open('http://localhost:4000/smaller.aspx', '_newtab');"; } if(result.equals("error") || result.equals("error")) { scriptval = "window.open('http://localhost:4000/errorpage.htm', '_newtab');"; } session["result"] = result; scriptval += "window.location.href = 'results.aspx';"; page.clientscript.registerstartupscript(page.gettype(), null, scriptval, true); }
see docs on clientscriptmanager.registerstartupscript
, specifically:
a startup script uniquely identified key , type. scripts same key , type considered duplicates. 1 script given type , key pair can registered page. attempting register script registered not create duplicate of script.
in case, type , key same in both of scripts register.
you uniquely identify them key, , register them separately. have keep in mind order of execution not guaranteed:
the script blocks not guaranteed output in order registered.
Comments
Post a Comment