Jquery- Get hiddenfield value in user control

get value of dynamic hiddenfield
In this post we will see how to get value of dynamically loaded hiddenfield value in jQuery. This happens when we use user control in page. In such cases id of hiddenfield get changed and so hiddenfield is not accessible from javascript or jQuery. We can't access hiddenfield using class selector also because hidden field don't have class property. In this case we can use "ClientIDMode" property and set it to ClientIDMode="Static". When we set ClientIDMode="Static" it will not set dynamic id to hidden field. And now we can access it in regular way.

Code to get hiddenfield value in user control [jQuery]

You can set ClientIDMode property of hidden field in user control like:

<asp:HiddenField runat="server" ID="hiddenValue" ClientIDMode="Static" value="Some Value" />

And in jQuery you can access it as like:
   <script type="text/javascript">
        $(document).ready(function () {
            $("#submitbtn").click(function () {
                var hiddenValue = $("#hiddenValue").val();
                alert(hiddenValue);
            });
        });
    </script>

Summary: After setting ClientIDMode="Static" it will not assing dynamic id to hiddenfield and we can access hiddenfield as usual.

2 comments :

  1. Replies
    1. Hi Christain,

      Thanks for the comments. The example I had given was just to show how we can set ClientIDMode. But as you mentioned, in example it looks like hiddenfield is not dynamic. I have modified the contents. Hope will make some sense.

      Important part is to set ClientIDMode of hiddenfield so that it will not assign dynamic Id to hidden field so that we can access hiddenfield in jquery easily.

      Delete