views:

243

answers:

2
+1  Q: 

alert box problem

how to show the alertbox first and then log out

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                    Response.Write("<script>alert('You are being logged out')</script>");

                   GoSignOut();
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
        Response.Redirect("~/Default.aspx");
    }

here if i dont call the GosignOut function i can see the alert box but the moment i put call the signout it does not show me the alertbox and simply signs out

So i was thinking if there is a way i can see the alertbox first and then when i press OK it should call signout function in my code behind... thanks

+1  A: 

use the clientclick of the server side button that calls this logout code, then return true

e.g.

<asp:Button id="button1" runat="server" OnClientClick="javascript:alert('You are being logged out');return true;" />

edit re thought what you want. try this, I have not tested with the use of session.abandon and the Signout (which may force you to the login page)

if (machineID.Count != 0)
            {

                checkMachineGrpState(machineID);
        else
                {
                   GoSignOut();
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "<script>alert('You are being logged out');window.location.href='" + ResolveUrl("~/Default.aspx") + "';</script>");
                }
    }

 private void GoSignout()
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
    }

if you need to allow the redirect to go to a page other than the login page (which is forced due to the SignOut) you can add something like the below to you web.config. I am not 100% sure you want to allow this as it would allow none authenticated users to access the page

<location path="whateverpage.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
Pharabus
how do i call this from my code behind... like from my else condition
appreciate the help
ahh, didnt see the else, what triggers the code you show, is it a button click? and if so why would they not be logged out?
Pharabus
no its not a button click, this is happening on page load.. thats the problem... the alert message has a button "OK" on it...
hmm, not sure you can do this, you are redirecting before any client script is rendering, in essence the browser will never see this if the else clause is hit, maybe you can redirect to a logged out page instead?
Pharabus
I have added another possiblity, as i mentioned I have not been able to test this with the Session.Abandon and Signout code you have
Pharabus
'System.Web.UI.Page.RegisterStartupScript(string, string)' is obsolete: 'The recommended alternative is ClientScript.RegisterStartupScript(Type type, string key, string script). http://go.microsoft.com/fwlink/?linkid=14202'
it is deprecated rather than obsolete but fair point, edited to use the recommended call, not sure it was worth a downvote and hopefully it did resolve your problem
Pharabus
i did not downvote it... i really appreciate the help..ok now thw code is still not working... it is not getting redirected now...
are you getting the popup though? just trying to establish how far in the process you can get.
Pharabus
yes i am.. the message box is coming
i think it is probably because you a calling FormsAuthentication.SignOut(); this would usually cause it to redirect to the login page defined in the web.config. I added a web.config section allowinf unauthenticated access but I am not sure whether you are able to alow this in your setup, doing this allowed it to work for me
Pharabus
i have been using this to separate the guest and admin folders... for security.. this simply authorizes it so this wont make a difference...
hmm, this code works fine for me, redirects where i want with the popup. After the popup on yours what exactly happens?
Pharabus
i just stay on the page... it does not get redirected...
so when i remove the signout function it should redirect right... its not doing that... is there any script i am missing
is the page default.aspx? are you not just redirecting to the same page? I have a test project with forms authentication working and the redirect seems fine on mine.
Pharabus
the thing is when i debug.. it does all the functios first and then pops up the box... but what it is not doing is after i click ok on the box it should redirect... right?? so i need to figure out a way to know what happens when OK is clicked...
no default is a different page...
this is strange, you are getting the popup so the script is registered and executing, not sure why window.location.href would not be working for you, what browser are you using, if you alert the ResolveUrl text what do you see. I think we are down to some specific config in your site that is preventing this, try hard coding some cript with a location.href change in it and see if that works.
Pharabus
hi, really sorry crappy IE allowinfg an invalid redirect, I ammended my code (notice the href =) this works in IE and FF (was not working in FF before)
Pharabus
Pharabus, no one voted down your post, you are +1 -0 for this post.
Allen
I know, it went to 0 (after being 1) for some reason, maybe was me confused. I noticed it went back to 1 and checked and it was +1 -0 as you say.
Pharabus
Pharabus this did the trick man... its doing the redirect also... now allen has given some good suggestions to which i will look into.. but ur thing worked man... thanks a ton...
+1  A: 

Response.write will write stuff out to the browser and when the browser loads the page, it parse that data (render html and execute js). No further communication or feedback is presented to the server (which is where your asp.net webforms code runs) without the browser contacting the server via a GET or POST (form submission, link, ajax).

Whats happening is you are directing output to the client but then immediately calling Response.Redirect, so the client never gets your output. Instead, it is just redirecting immediately and then displays the default.aspx page.

What you could do is still do the redirect but pass some data to the client (the querystring would be a way to do this) and then the default.aspx page would know to look for that data. If that data is found, it would complete any action necessary. In your case, you want it to alert the user.

One way of doing this is by redirecting to "~/Default.aspx?alert=1". In default.aspx, on pageload, it would look to the querystring and see if the alert key is there with the value of 1. If that was the case, it'd perform your alert.

Allen