ASP.NET set hiddenfield a value in Javascript

asp.net, c#, javascript

Solution

Prior to ASP.Net 4.0

ClientID

Get the client id generated in the page that uses Master page. As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like `ctrl_`.

This can be resolved by using `<%= ControlName.ClientID %>` in a page and can be assigned to any string or a javascript variables that can be referred later.

var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');

- Asp.net server control id will be vary if you use Master page.

ASP.Net 4.0 +

ClientIDMode Property

Use this property to control how you want to generate the ID for you. For your case setting ClientIDMode="static" in page level will resolve the problem. The same thing can be applied at control level as well.

Problem

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this? Javascript: ``` document.getElementById('hdntxtbxTaksit').value = ""; ``` HTML: ``` <asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField> ``` error : "Unable to get value of the property \'value\': object is null or undefined"

Original source