I want to store session info(from asp) in local storage and access in JS and its working but when for the first time I login the (local storage key value) "userStatus" which is "user" does not store in the local storage even no local storage is NOT created …. BUT when I go back from any page(after log out) to login and LOGIN AGAIN then the local storage is created... anyone have any idea what I am missing (is it post back issue??)
button Login
<div class="d-grid gap-2 pt-2">
<asp:Button ID="Button1" runat="server" class="btn btn-secondary " Text="Log In" OnClick="btnLogIn_Click" OnClientClick="checkStatus();" />
</div>
asp Code where i am passing Session["role"] = "user" from login page.cs to masterpage variable
if (dr.HasRows)
{
if(dr.Read())
{
//Response.Write("<script> alert( 'the user : "+dr.GetValue(8).ToString()+" Successfully Logged in')</script>");
Session["username"]=dr.GetValue(8).ToString();
Session["fullname"] = dr.GetValue(0).ToString();
Session["role"] = "user";
Site1.PublicSession=HiddenField1.Value = Session["role"].ToString();
Session["status"] = dr.GetValue(10).ToString();
}
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnSucc()", true);
Response.Redirect("homePage.aspx");
}
else
{
// Response.Write("<script> alert('Invalid Login or Password ')</script>");
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnFailure()",true);
HiddenField1.Value = null;
}
my Js function in the master page header
<script type="text/javascript">
function checkStatus() {
var printSession = '<%=PublicSession%>';
console.log(printSession);
if (printSession) {
localStorage.setItem("userStatus", printSession);
}
}
</script>