Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have my logout in listitem..

<li  ><a>Log Out</a></li>


I need to write log out functionality on clicking log out..

I have tried like this..

<li onclick="logout" ><a>Log Out</a></li>


C#
protected void logout(object sender, EventArgs e)
   {

       Session.Clear();
       Response.Redirect("login.aspx");

   }


But its not working..

How to do this..

thanks in advance..
Posted
Comments
Nivedita_Parihar 5-Jun-14 5:17am    
Did u tried to debug this. what error it is throwing?
Bhushan Patki 5-Jun-14 5:18am    
Try these
http://www.codeproject.com/Questions/361938/Logout-code-in-asp-net-csharp-for-admin-which-have

Hello friend, try this:
HTML
<a href="logout.aspx">Log Out</a>

Add a new web page - logout.aspx and write the below code in Page_Load() event:
C#
//Cancels current session
Session.Abandon();
Response.Redirect("login.aspx");
 
Share this answer
 
v2
Comments
SRS(The Coder) 5-Jun-14 5:50am    
yes this is the best approach to do.

Again you can try the solution i posted if you want to do the same on client-side.
add OnClick event as logout and include runat="server"

ASP.NET
<li  runat="server"  OnClick="logout">Log Out</li>
 
Share this answer
 
v3
Hello Priya Darshan,

It is a obvious fail attempt to call a server method through client side element.

You are using client side html elements as list which is a
HTML
<li>
element, so you have to call a client-side method only in JavaScript.

In your case you can call this page method in JavaScript making a ajax call (check with jquery
JavaScript
.ajax()
method.), then call that method in the list item click.

OR else you can create a web method to be called through JavaScript, then call that method in the list item click.

Again to add click event handler to li element add a ID to that element let say 'liTest', now add the click event as below :-

JavaScript
$('#liTest').on('click', function(){
  Logout();
});


Reference : -

http://api.jquery.com/jquery.ajax/[^]

[^]

Hope you will have some idea by referring this explanation.
 
Share this answer
 
v2
 
Share this answer
 
v3
@design page(.aspx)

<script src="../Script/jquery-1.6.1.min.js"></script>


<ul><li onclick="logout()">Logout</li></ul>



function logout() {
$.ajax({
type: "POST",
url: "WebMethod.aspx/callservice2",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
//Here success code;
},
failure: function (response) {
//Here failure code;
}
});
}



@code behind(.aspx.cs)

using System.Web.Services;

[WebMethod]
public static string callservice2()
{
string retValue = string.Empty;
try
{
retValue = "Hi";
return retValue;
}
catch (Exception ex)
{
return "Error";
}
}


***Please try this and give your feedback***
 
Share this answer
 

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