How do i achieve to pass a string from code-behind c# to local storage by using javascript?
I tried to pass the string over an HiddenField and Session cache, but i had no success by retrieving the string in javascript to store it in the local storage. I got an onclick-function in c# to receive a string and write it in the session cache. After that i try to retrieve the string over a function in js where i read the session cache and write it in the local storage. But the result is always empty, except of some tests where i filled the session cache within the page-load function with a static string.
Do anybody suspect the problem in my solution?
I'd appreciate your input.
Edit:
First i push Button "btn1" to write myString into the HF and then "btn2" to read it in JS and storage it in the 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; }
A hidden field will work. Assuming web forms, use an asp:Hidden field:
<asp:HiddenField ID="LocalStore" runat="server" ClientIDMode="Static" />
Then, you can retrieve it via:
document.getElementById("LocalStore").value
or JQuery:
$("#LocalStore").val();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.