Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the the value of an asp.net hiddenfield in a typescript file

Here is the control in the aspx page..

<asp:HiddenField ID="hdnGOfromLocalStorage" runat="server" Value="-1" />

Here is the code that is not workingin the Typescript file

let xxx: string = document.getElementById('<%= hdnGOfromLocalStorage.ClientID %>');

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your expression and code does not really make sense, since you attempting to assign the value to itself???

Your question is one of you attempting to get a value from the hidden field, not set a value.

Hence the following code should work:

 <asp:HiddenField ID="hdnGOfromLocalStorage" runat="server" Value="-1" />

 <asp:Button ID="cmdTest" runat="server" Text="Show Hidden value"
            OnClientClick="mytest();return false;" />

        <script>

            function mytest() {

                var MyValue 
                MyValue = document.getElementById('<%= hdnGOfromLocalStorage.ClientID %>').value

                alert("Value of hidden field = " + MyValue)

            }

        </script>

And when we run the above, we see this:

enter image description here

So, your expression does not retrieve any value, and looks to be an attempt to "assign" a value to the hidden field, which of course is a different question.

To assign a value to the hidden field, the following code would work:

document.getElementById('<%= hdnGOfromLocalStorage.ClientID %>').value = "Hello world"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading