Muthu
Muthu

Reputation: 165

How to get HTML Local Storage value in ASP.NET PageLoad event?

text value am assigning to Local storage like :

 <asp:TextBox runat="server" class="field medium" ID="TextTreatement" /><br />
 localStorage.setItem("TextTreatementKey", TextTreatement);// here am textbox value assign to local Storage.

I have declared Hidden field like :

<asp:HiddenField ID="hdnSelectTestID" runat="server" / 

my java script like :

 <script type="text/javascript"> 
window.onload = function (){ 
  document.getElementById('<%=hdnSelectTestID.ClientID %>').value = localStorage.getItem("hdntestValue"); }

 protected void Page_Load(object sender, EventArgs e)
  {
      string val =hdnSelectTestID.Value // here am not able asign value. its coming string.empty 
  }

Upvotes: 1

Views: 7652

Answers (2)

user5487197
user5487197

Reputation:

Maybe its a little bit late but I think you are not taking the right aproach, whatever you are trying to do on Page_Load will execute first, thats why that method "cant" find anything, because it hasnt been created yet.

Maybe you sould try using another JS after the page is created so that you can manipulate variables that you have already created and worked with.

Dont know if its the nicest way but maybe you can store whatever you want to manipulate from server on a session variable lets say Session["myvariable"]= "whatever" an then you can use it through js with <%Session["myvariable"]%>

Upvotes: 0

mason
mason

Reputation: 32704

You can't get it on initial page load, which is what it sounds like you want to do. Keep in mind how web servers and clients work. The server side executes first, then sends the resulting HTML/CSS/JS to the client. Therefore, you can't get values from the client side JavaScript before the initial page load. An alternative is to store the info in a cookie, or use AJAX to communicate back to the server, or pick the value up after a postback

Upvotes: 2

Related Questions