Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

How to show the message and redirect to other page in ASP.NET

4.04/5 (18 votes)
20 Nov 2011CPOL 63.8K  
We can simply do this by calling JavaScript in code behind.
The below code snippet is used to show the message and redirect to other page:

C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Access Denied. Please contact Administrator for access to this section.');location.href = 'login.aspx';", true);


The below code snippet is used to show the message and redirect to other page with parameters:

C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Data updated successfully. You will now redirected to search page.');location.href = 'somepage.aspx?name=" + name + "&address=" + address + "&state=" + state + "';", true);


The below code snippet is used to show the message and redirect to other page with parameters based on user confirm:

C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "var r = confirm('Data updated successfully. You will now redirected to search page.'); if (r == true) var str= 'somepage.aspx?name=" + name + "&address=" + address + "&state=" + state + "'; location.href = str ;", true);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)