Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All ,

VB
messagebox("you are redirected to home page")
response.redirect("home.aspx")



in this code Message box is not shown. it is directly load the Home page.


here messagebox is a function which has Alert script.

How to redirect home page after the messgebox??



Regards,
Pal
Posted
Updated 22-Mar-19 5:50am

The java-script message boxes are typically hooked on page_unload event. Like the one here:

A Windows Form like MessageBox for ASP.NET Website[^]

so if you call response.redirect the page_unload event for the earlier page will not get called after calling the messagebox function and that wont work. IF you wanto do achieve this then you should do this:

C#
Response.Write("<script>alert('you are redirected to home page')</script>");
Server.Transfer("home.aspx")


or even this will work

C#
messagebox("you are redirected to home page");
Server.Transfer("home.aspx")


both have some minor visible difference as on when the messagebox will appear on screen.
 
Share this answer
 
v2
in code behind you can call javascript function

in code behind

C#
string script = "Redirect();";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Redirect", script, true);



And in javascript

XML
<script type="text/javascript">

function Redirect()
{
alert("Message");
    window.location="http://www.newlocation.com";
}
 
Share this answer
 
v3
Hi,
Try this code :
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "InsertedMessage", "alert('you are redirected to home page');", true);
Response.Redirect("home.aspx");
 
Share this answer
 
Comments
sachin4dotnet 27-Nov-12 9:45am    
not working
Krushna Mohanta 26-Dec-13 0:26am    
not working
It is not so complex you have many options to do that
People says that it cannot because asp throws just after response.redirect method
But Try This i done this when my code was in ASP


C#
Response.Write("<script>alert('Hemml')</script>");
              Response.Write("<script>window.location.href='ANyPage.aspx';</script>");
 
Share this answer
 
Comments
DalachhaSolanki 23-Feb-16 11:36am    
it's work fine..
ScriptManager.RegisterStartupScript(this, this.GetType(), "SetMessage" + DateTime.Now, "SetMessage('','Red');alert('Alert Before Redirection');location.href="/KB/answers/NewPage.aspx";" , true);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900