ASP.NET set hiddenfield a value in Javascript

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”

Answers:

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.

Method 1

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.

Method 2

asp:HiddenField as:

<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />

js code:
$("#hfProduct").val("test")

and the code behind:
hfProduct.Value.ToString();

Method 3

  • 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");

Method 4

document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value

The id you set in server is the server id which is different from client id.

Method 5

try this code:

$('hdntxtbxTaksit').val('test');

Method 6

I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you’ve marked the field as runat="server".

E.g., if your JavaScript code is in an aspx file (not a separate JavaScript file):

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

If it’s in a separate JavaScript file that isn’t rendered by the ASP.Net stuff, you’ll have to find it another way, such as by class.

Method 7

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.

Method 8

I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:

.cs file code:

ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);

and then use following code in JS:
document.getElementById(hdntxtbxTaksit).value= "";

Method 9

Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x