OnClick Event To Close Session in ASP.NET

OnClick Is Not Working When I Clicked On Button

I have Provided Button Named Logout and Written an Function That needs to be performed when that button is clicked.

But when i clicked on it it's not executing the function, i checked by placing breakpoint, but breakpoint is not activated when i clicked on Logout button.

<button type="submit" class="btn btn-warning" style="margin-top:-280px; margin-left:1100px;" onclick="Logout()">Logout</button>

<script>
    function Logout() {
        @Session["IsLoggedIn"] = false;
        @Session["RoleId"] = null;
        @Session["LoyalityPoints"] = null;
        return RedirectToAction("IndexSeachBeforeLogin");
    }
</script>

Upvotes: 2

Views: 217

Answers (1)

Bharat
Bharat

Reputation: 1205

You are using server side varaibles sunch as @Session["IsLoggedIn"] to assign the value in javascript.

However, when you use such case the value of @Session["IsLoggedIn"] variable get's replaced during the page rendering e.g if @Session["IsLoggedIn"] value is true then it would be

true = false;

This would result in javascript error and hence your code is not working.

Ideally to clear the variables values of logged in session, you must clear them from server side and not at client side.

Upvotes: 2

Related Questions