While working with master pages in .NET, I encountered an issue where I couldn't retrieve values using ID's because .NET automatically adds its own id values. To learn more about this problem, check out a helpful article called Can't get JQuery to work in Master Page.
To address this, I added classes to the relevant fields. However, I faced a challenge when dealing with a hidden field:
<asp:HiddenField ID="softwareSelected" value="" runat="server" />
Adding a class to this field wasn't possible, so after some research, I found that people resolved it by utilizing the clientID
property.
I attempted this solution:
var myHidden = document.getElementById('<%= softwareSelected.ClientID %>');
console.log(myHidden.value);
alert(myHidden.value);
Referring to another answer provided on ASP.NET set hiddenfield a value in Javascript, I continued trying to make it work. However, I faced difficulties as it returned only empty alerts and console logs.
Seeking further guidance, I placed the following code snippet higher up in my file:
<asp:HiddenField ID="softwareSelected" ClientIDMode="static" value="test" runat="server" />
Despite setting the ClientIDMode to 'static', the output remained as undefined
. In an attempt to overwrite this value, I tried the following:
var myHidden = document.getElementById('<%= softwareSelected.ClientID %>').value;
console.log(myHidden.value);
myHidden.value = "tester123";
console.log(myHidden.value);
Unfortunately, even after this adjustment, the result was stillundefined
. This situation has been quite frustrating for me!