views:

714

answers:

2

Hi, I have a page that's derived from a master page. On this page, I have a hiddenfield ("hfUser"). How can I access this "hfUser" control and get/set its value using JQuery? I've tried variants of this:

$(document).ready(function() {
    var test = $("#hfUser").val();
    alert(test);

});

but test = undefined. I'm guessing I've got the selector wrong, but I don't know how to get an asp hiddenfield. Any ideas?

Thanks

+4  A: 

If you are using Asp.net controls, the server will mangle the control ids. It adds a bunch of extraneous control tree hierarchy information into the id. You need to reference what that acutal id is that's getting rendered, which is availble with the ClientID property on the control (hfUser.ClientID) or access your control in a different, more roundabout way, like find the controls parent, then search through its children to find your control.

If you don't have to use the asp.net HiddenField control, try just using a regular old html input.

Doug R
+1  A: 

If the hidden field is an ASP.NET control, check out this blog post to help you with jQuery selectors for ASP.NET controls

http://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/

T B