Is there a way to pass a string from C# code-behind to local storage using JavaScript?
I attempted to pass the string through a HiddenField and Session cache, but was unsuccessful in retrieving the string in JavaScript to store it in local storage. I have an onclick function in C# that receives the string and writes it to the session cache. Then, I try to retrieve the string using a JavaScript function that reads the session cache and writes it to local storage. However, the result is always empty, except when I manually fill the session cache with a static string within the page load function.
Could anyone identify any issues with my solution or offer suggestions?
I welcome any input you may have.
Edit:
I click Button "btn1" to write myString into the hidden field and then click "btn2" to read it in JavaScript and store it in local storage.
HTML code
<asp:HiddenField ID="hiddenVar" runat="server" ClientIDMode="Static" />
<asp:Button ID="btn1" runat="server" OnClick="writeInHiddenField" />
<asp:Button ID="btn2" runat="server" OnClientClick="writeHfInLS()" />
<script>
function writeHfInLS() { window.localStorage.setItem('key', document.getElementById("hiddenVar").value); }
</script>
C# function in code behind
writeInHiddenField() { hiddenVar.Value = myString; }