1

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

2
  • When you check the value (i meain st = hdn.value? On page load? If is, it is after post back or not? How do you check value using linebreaker? What's more, the ID of element is invalid. The ID property tells us about server control id, but client id is different (id in input field, auto-generated). Commented Aug 7, 2014 at 6:02
  • 1
    try document.getElementById('<%= hdn.ClientID %>').value = "helo"; Commented Aug 7, 2014 at 6:04

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.