Ok so this is my ASP code of Login page where on success I am creating one of the session which is Session["role"]="user" and i want to pass that value to HiddenField1 and then I am trying to get this value in JavaScript but when i console.log HiddenField value I am always getting null even though I've tried almost all suggested solutions...if anyone can solve the issue then please give a way
Asp code
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";
HiddenField1.Value = Session["role"].ToString();
Response.Write("<script> alert(" HiddenField1.Value ")</script>");
Session["status"] = dr.GetValue(10).ToString();
}
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnSucc()", true);
Response.Redirect("homePage.aspx");
}
JAVASCRIPT in the masterpage Head
<script type="text/javascript">
var array_store;
array_store = document.getElementById("HiddenField1").value;
console.log(array_store);
</script>
And this is my hiddenField
<asp:HiddenField ID="HiddenField1" runat="server" />
and this the ERROR
homePage.aspx:28 Uncaught TypeError: Cannot read properties of null (reading 'value')
CodePudding user response:
You can use a handler (.ashx file) for returning session value and then a ajax call in the front side to get that value , or a method with [WebMethod] attribute in backend side and a ajax call at front side to get session value.
CodePudding user response:
Ok, so you might have spend some time making it CRYSTAL clear that you have a master page here?
however, you have this code:
HiddenField1.Value = Session["role"].ToString();
Response.Write("<script> alert(" HiddenField1.Value ")</script>");
Session["status"] = dr.GetValue(10).ToString();
}
ScriptManager.RegisterStartupScript(this, GetType(), "popup", "MyAlertFunOnSucc()", true);
Response.Redirect("homePage.aspx");
You do realize what a response redirect does, right? It blows out the current page, the current class, and the current controls. You then say please forget about this page, and now re-direct to a WHOLE NEW page. (home page).
So, how can you inject a script, but THEN decide to dump the whole current page not send it to the client, and THEN re-direct to a whole different page????
What this means is that you need to put/place/have that code that sets the hidden control, and the register script MOVED to the home page code. So, on home page (is PostBack = false), you can set the hidden field, and THEN do your register script.
but, with your current setup, by executing a redirect to a whole new page, your register script, and in fact even your setting of the hidden field - both make no sense at all if after setting the hidden field, and tossing in a scripts to run?
YOU THEN SAY FORGET ABOUT ALL THAT AND TRANSFER to a whole new and different page.
So, move your code that sets the hidden field, and register script to the load of the home page (page load event).