URGENT - Show message and redirect to a page

B

bnob

In a Button clik event I have this code at the end of the event

Response.Redirect("Page.aspx")

But in this event I must show a message before redirect to the
Page.aspx. I use to show Message this

System.Web.HttpContext.Current.Response.Write("<SCRIPT
LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("alert('A message')" &
vbCrLf)
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

but I don't show the message alert?? If a delete
Response.Redirect("Page.aspx") line It works!!

Any idea to show message and then redirect to a page.aspx ??
 
K

Karl Seguin

Bnob:
Your best solution is to hook up an onClick event to the button:

Sub Page_Load
if not page.ispostback then
btnClick.Attributes.Add("onClick", "alert('a Message');")
end if
End Sub

The reason your code doesn't work is that when the button is click, it posts
back to the server. At this point you Response.Write the <script stuff, but
that never gets sent to the browser because you response.redirect. If the
method above isn't satisfactory, the only alternative would be, on postback,
output the your javascript, with additional javascript code to redirect the
client to the page...not a nice alternative (and not sure why you'd need
it).

Karl
 
B

bnob

Karl Seguin avait prétendu :
Bnob:
Your best solution is to hook up an onClick event to the button:

Sub Page_Load
if not page.ispostback then
btnClick.Attributes.Add("onClick", "alert('a Message');")
end if
End Sub

The reason your code doesn't work is that when the button is click, it posts
back to the server. At this point you Response.Write the <script stuff, but
that never gets sent to the browser because you response.redirect. If the
method above isn't satisfactory, the only alternative would be, on postback,
output the your javascript, with additional javascript code to redirect the
client to the page...not a nice alternative (and not sure why you'd need
it).

Karl


It doesn't work because the alert message don't be showed in all case.

I make a test and if it's false, I show the alert message, if true I
don't show the message. With attributes "onclick" the alert message is
always showed
 
K

Karl Seguin

bnob:
In that case you either implement your "test" on the client - which I don't
think is a particulary good idea, or you use the alternative method I
suggested. The problem is that it's one extra page hit. You could also
show the message on Page.ASPX (the page you redirect to) when it loads...


Response.Redirect("Page.aspx?showMessage=true");

in Page.aspx, on load, check if showMessage = true, if so, display the
message.

Karl
 
A

Anders Norås [MCAD]

The HttpResponse.Redirect emits the HTML code need to redirect to the
redirection url. If smart navigation is enabled and a post back has been
done the following HTML is emitted:
<BODY><ASP_SMART_NAV_RDIR url="Page.aspx"></ASP_SMART_NAV_RDIR></BODY>

Otherwise the following code is emitted:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="Page.aspx">here</a>.</h2>
</body></html>

An HTTP redirect header is also sent, so the emitted HTML won't be displayed
in most modern browsers.
To show your message before the redirection cannot use
HttpResponse.Redirect, instead you must emit the HTML code to display the
message and then redirect to the target url.

This code can look like this:
<html><head><script language="JavaScript">alert("A
message");location.replace("Page.aspx");</script></head><body></body></html>

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top