Search code examples
javascriptc#asp.netmaster-pages

ASP.NET hiddenField get value using ClientID as using master pages using javascript


I'm using master pages in .NET and I've discovered that I can't use ID's to retrieve values because .NET adds in it's own id values. See this very helpful article! Can't get JQuery to work in Master Page

So I went through and added classes to all the relevant fields, however I ran into a problem when it came to my hidden field <asp:HiddenField ID="softwareSelected" value="" runat="server" />

I can't add a class into it, and after more Googling I saw that people fix it by calling clientID.

So I tried this:

var myHidden = document.getElementById('<%= softwareSelected.ClientID %>');
console.log(myHidden.value);
alert(myHidden.value);

Using this answer: ASP.NET set hiddenfield a value in Javascript

However it isn't returning any value, just an empty alert and console.log.

Could someone point me in the right direction? This is driving me nuts!

EDIT

I set this higher up in my code:

<asp:HiddenField ID="softwareSelected" ClientIDMode="static" value="test" runat="server" />

By putting the ClientIDMode to static I'm getting back undefined

Then I try and overwrite this value like this:

var myHidden = document.getElementById('<%= softwareSelected.ClientID %>').value;
console.log(myHidden.value);
 myHidden.value = "tester123";
 console.log(myHidden.value);

But I still get undefined :/


Solution

  • I tested that and worked for me. I think your javascript codes are in an external file. If this is you must know that

    <%= softwareSelected.ClientID %>
    

    Only works in you .aspx file.

    And if you js scripts are in .aspx file please test this:

    var myHidden = document.getElementById('JsContent_ softwareSelected').value; 
    

    If this does not work you must search some where else to you problem.