Reputation: 468
This is the hidden field and the javascript.
<asp:HiddenField ID="hdn" runat="server" />
<script type="text/javascript">
document.getElementById("hdn").value = "helo";
</script>
And i tried to access the hidden field value in the .cs file as string st = hdn.value
.
But it shows null when i check the value using linebreaker
Upvotes: 1
Views: 1146
Reputation: 148110
Use ClientID
instead of server id and also make sure that javascript is executed after the hdn
field being added to DOM, you can put the script
tag just before the closing body tag.
document.getElementById("<%= hdn.ClientID %>").value = "helo";
If you have .net framework 4 and above you can also set ClientIDMode to static
to keep the server id on client unchanged.
HTML
<asp:HiddenField ID="hdn" runat="server" ClientIDMode="static" />
Javacript
<script type="text/javascript">
document.getElementById("hdn").value = "helo";
</script>
Upvotes: 2