问题描述:

I have one logout button and it should work for 3 different logout functionalities i.e- User logout,Logout from Facebook,Logout from Google.I am explaining my code for 3 logout functionalities below.Please help me to write all inside one click event.

My logout button is given below.

<li><a href="#" runat="server" onServerClick="fun_signout" id="sign_out">Sign Out</a></li>

Local user logout:

protected void fun_signout(object sender, EventArgs e)

{

Session.Remove("User_Email");

Session.RemoveAll();

Response.Redirect("UserLogin.aspx");

}

Logout using Facebook:

protected void fun_signout(object sender, EventArgs e)

{

FaceBookConnect.Logout(Request.QueryString["code"]);

}

For google i am using JavaScript.So for that my Logout button should like below.

<li><a href="#" target='myIFrame' onClientClick="myIFrame.location='https://www.google.com/accounts/Logout'; startLogoutPolling();return false;" id="sign_out" >Sign Out</a></li>

And code for logout from google is explained below.

<script type="text/javascript">

function startLogoutPolling() {

window.location.href = "UserLogin.aspx";

loggedIn = false;

}

</script>

Here my requirement is i want to keep logout button same and implement all above functionalities .Please help me to do this.

网友答案:

Firstly, use a server sided ASP Control like the asp:Button. If you accept that it will do a PostBack (even on Google sign out) then you can do:

<asp:Button ID="bSignOut" runat="server" OnClick="bSignOut_Click" />

On the Page_Load, assign CommandName depending on your Login type:

this.bSignOut.CommandName = "Regular"; // or "Google" or "Facebook"

Then in your event you can determine what you do:

protected void bSignOut_Click(object sender, EventArgs e)
{
  Button bSignOut = (Button)sender;
  if(bSignOut.CommandName.Equals("Regular"))
  {
        Session.Remove("User_Email");
        Session.RemoveAll();
        Response.Redirect("UserLogin.aspx");
  }
  else if(bSignOut.CommandName.Equals("Google"))
  {
        string script = "myIFrame.location='https://www.google.com/accounts/Logout'; startLogoutPolling();"
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "GoogleSignOut", script, true);
  }
  else if(bSignOut.CommandName.Equals("Facebook"))
  {
        FaceBookConnect.Logout(Request.QueryString["code"]);
  }
}

If you don't want the PostBack on the Google signout you have no choice than creating a second button and toggle visibility or attaching the JavaScript (including return false) to the button on the Page_Load.

相关阅读:
Top