document.getElementById('hdntxtbxTaksit').value = "";
<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”
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Prior to ASP.Net 4.0
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
This can be resolved by using
var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
- Asp.net server control id will be vary if you use Master page.
ASP.Net 4.0 +
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.
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />
and the code behind:
First you need to create the Hidden Field properly
<asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>
Then you need to set value to the hidden field
If you aren’t using Jquery you should use it:
document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";
If you are using Jquery, this is how it should be:
$("#<%= hdntxtbxTaksit.ClientID %>").val("test");
The id you set in server is the server id which is different from client id.
try this code:
I suspect you need to use
var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;
My understanding is if you set
controls.Visible = false during initial page load, it doesn’t get rendered in the client response. My suggestion to solve your problem is
Don’t use placeholder, judging from the scenario, you don’t really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without
runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don’t set
visible = false. Placeholder won’t have any display anyway, Set the visibility of that placeholder using css. Here’s how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set
control.visible = false, it doesn’t get rendered in the client response.
.cs file code:
ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);
and then use following code in JS:
document.getElementByName('hdntxtbxTaksit').value = '0';