Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.04/5 (18 votes)
20 Nov 2011CPOL 60.4K   17   9
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
DamithSL5-Nov-14 17:49
professionalDamithSL5-Nov-14 17:49 
GeneralNice Tip - Voted 4 Pin
Matt U.23-Aug-12 4:55
Matt U.23-Aug-12 4:55 
GeneralReason for my vote of 5 Good tip Pin
Pranit Kothari8-Jan-12 21:00
Pranit Kothari8-Jan-12 21:00 
GeneralReason for my vote of 3 Its a common sense Pin
dennis-ye1623-Nov-11 15:34
dennis-ye1623-Nov-11 15:34 
GeneralReason for my vote of 5 thanks for the tip! Pin
beginner201122-Nov-11 14:50
beginner201122-Nov-11 14:50 
GeneralReason for my vote of 5 Good Pin
iambics22-Nov-11 7:49
iambics22-Nov-11 7:49 
GeneralReason for my vote of 5 Nice little trick, certainly could b... Pin
BrianBissell22-Nov-11 5:35
BrianBissell22-Nov-11 5:35 
GeneralReason for my vote of 5 Good one Pin
demouser74321-Nov-11 2:28
demouser74321-Nov-11 2:28 
GeneralReason for my vote of 5 Helpful in my current project. Pin
Varun Sareen18-Nov-11 0:49
Varun Sareen18-Nov-11 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.