Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I'm having trouble trying to show a message (from the server side to the client side) before I do a response.redirect to another page.
The thing is that I put the code to show the message but it never appears in the page, it does the redirect but it doesn't show the message.

The code I'm using to show the message is this:

VB
sScript.AppendLine("<script type=""text/javascript"">")
sScript.AppendLine("  alert('La operacion no ha sido exitosa');")
sScript.AppendLine("</script>")
ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertaJS", sScript.ToString, False)


sScript is a stringbuilder



Is there any other way to showing messages from server side to client side?

Thanks!
Posted

When you use a Response.Redirect ASP.NET adds header to issue a 302, object moved. Since the browser processes the header, and requests the new url, before the page actually loads this method won't work for you.

You can add JavaScript after the alert such as

window.location.href = 'http://my new location';
 
Share this answer
 
Comments
Sheerim 30-Oct-11 8:33am    
Thnks!!! It works perfectly and does exactly what i needed :)
Hi Sheerim,
Show Message Before Response.Redirect[^]
I hope it helps you.
 
Share this answer
 
v2
Comments
Amir Mahfoozi 25-Oct-11 8:33am    
Please correct your link before some people attack you violently :-&
srinivas vadepally 25-Oct-11 8:38am    
Sorry Amir,just find it now
place your message in a page with this header :
HTML
<meta http-equiv="refresh" content="5;url=Default.aspx" />
and redirect them to this page.
This header will redirect them to the "default.aspx" after 5 seconds but before that they can see your message.

example :

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Access Denied</title>
    <meta http-equiv="refresh" content="5;url=Default.aspx" />
    <link rel="Stylesheet" type="text/css" href="default.css" />
</head>
<body dir="rtl">
    <form id="form1" runat="server">
    <div class="error">
        <img alt="error" src="images\cross1.png" width=50 height=50 />
       access denied.</div>
        <br />
        <br />
        <a href="Default.aspx">go to home page</a>


    </form>
</body>
 
Share this answer
 
v2
Comments
[no name] 25-Oct-11 11:40am    
So your solution would involve create a new page for each message that may need to be displayed, redirecting uses there, then on to yet another page? Do you see any problems with this?
Amir Mahfoozi 25-Oct-11 15:34pm    
It is just a clue. Having a dynamic part for messages isn't a complicated problem and is very easy to solve. Isn't it ?

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