Answered by:
response.redirect executing before alert message

Question
-
User219039814 posted
I have a condition depending on which an alert message gets displayed and then page is redirected.
but alert message is not getting displayed and the page is directly redirected.
if (String.IsNullOrEmpty(sname))
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Checking');", true);
Response.Redirect("Resources.aspx");
}Saturday, June 30, 2018 5:02 AM
Answers
-
User-1171043462 posted
Refer
ASP.Net: Show JavaScript Alert Message Box and Redirect to another page or website
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 30, 2018 11:29 AM -
User283571144 posted
Hi vijaylakshmim,
As @mike says, since you directly use response.redirect method to redirect to another page in code-behind, your javascript will not add and run in previous page.
So you will find the alert not work.
I suggest you could use javascript’s winodws.location.herf method to redirect to another page in the client side.
More details ,you could refer to below codes:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebFormLearning.Test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" /> //just a button in the page </div> </form> </body> </html>
Code-behind:
public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('checking');window.location.href='Resources.aspx';", true); } } }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 2, 2018 9:32 AM
All replies
-
User-821857111 posted
Yes - that's how the browser works. If a redirect is issued as part of the response, the browser does not wait for anything.
Saturday, June 30, 2018 9:11 AM -
User-1171043462 posted
Refer
ASP.Net: Show JavaScript Alert Message Box and Redirect to another page or website
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 30, 2018 11:29 AM -
User283571144 posted
Hi vijaylakshmim,
As @mike says, since you directly use response.redirect method to redirect to another page in code-behind, your javascript will not add and run in previous page.
So you will find the alert not work.
I suggest you could use javascript’s winodws.location.herf method to redirect to another page in the client side.
More details ,you could refer to below codes:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebFormLearning.Test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" /> //just a button in the page </div> </form> </body> </html>
Code-behind:
public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('checking');window.location.href='Resources.aspx';", true); } } }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 2, 2018 9:32 AM