user35
user35

Reputation: 468

Passing value to the hidden field from java-script not working

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

Answers (1)

Adil
Adil

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

Related Questions