Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET set hiddenfield a value in Javascript

Tags:

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"

like image 811
ozkank Avatar asked Nov 19 '12 13:11

ozkank


People also ask

Can ASP.NET be used with JavaScript?

ASP.NET requires the use of the client-side Javascript, as that's how ASP.NET handles its Events via PostBacks.

How can access hidden field in ASP.NET code behind?

Right click on project and select Add-->Add New Item and select Web Form. Add a new Web Form called "Default. aspx". Now, drag and drop the HiddenField Control on the page.

How can store hidden field value in jQuery?

#1 jQuery Code to set hidden field value by IDval(); // this gives textbox value $("#myInputHidden"). val(getTxtValue); // this will set hidden field value alert(getValue); Above jQuery code will take textbox value into the variable getTxtValue and using . val() set its value to our hidden field.


1 Answers

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.

like image 161
Murali Murugesan Avatar answered Oct 17 '22 02:10

Murali Murugesan