javascript alert function

  • Thread starter Thread starter Kenneth
  • Start date Start date
K

Kenneth

Hi,

In a document I have three lines that detects and
redirects to another page is a session variable is 0

If Session("Customer_ID")=0 Then
Response.Redirect("myPage.aspx")
End If

What I really would like to have is a notification for the
users what happened before they were redirected and in
traditional asp I used the javascript alert function, but
how does the code go to implement this in asp.net above
the line Response.Redirect("myPage.aspx")?

TIA

/Kenneth
 
You have to be mindful of where your code is running when you are using
ASP.NET. In your example, since you are using the Session object, you are
running on the server. Your web server has no way of popping up an alert
dialog on the user's machine. Instead, it has to write JavaScript code to
the client, which can then execute this code and give the user an alert. So,
you need to change your approach. If you need the alert, you will need to
feed the information to the client, and have the entire check take place
there, in which case you lose access to your session variable (stored on the
server), so the only comparison you will be able to do is based on something
on that form or else something that you write out to the client machine.

If you want to get clever, it's also possible that you could redirect to a
new page that serves the same purpose as the alert - giving them a button to
proceed with the redirect, and a button that just executes a history.back
script to bring the user back to the previous page.
 
Chris,

With all respect, in an oldfashioned .asp file I have the
following code:

<% if Session("UserName") = "" then %>
<script language="javascript">
alert('Session Timeout has expired. You
have to log in again!');
top.location.href = "login.asp";
</script>
<% end if%>

It also runs on the server.

Can't I use the Page 'RegisterStartupScript' method to
achive the same effect?

/Kenneth
 
Back
Top