views:

1259

answers:

2

Is it possible to get a particular value out of viewstate using JQuery....

I'm working on a custom control. It uses jquery, embedded into the control itself to manipulate the value in a text box... I need to implement a minimum and maximum values... the properties are set up in the control, and are stored in viewstate... Instead of using hidden input fields, i'd much rather just pull the info out of viewstate and use it that way.... is it at all possible?

Thanks

+2  A: 

The short answer is that you can do it under some circumstances, but it really isn't a good idea.

If you are writing the control for others to use, there's no way ViewState is going to work. By default, ViewState is Base64 encoded which is easy enough to decode, but users of your control may want to encrypt their ViewState data, in which case you would be hosed.

If you are writing the control for your own consumption and you only need to read from ViewState, you could do so, but I wouldn't recommend it unless you find a well-debugged library to parse it for you. The format is a bit hairy (see ViewState: All You Wanted to Know and Understanding ASP.NET ViewState for more details).

As you mentioned, using a standard hidden field is a fine alternative, or you could inject another block of javascript into your page to set the variable values at whatever point the control sets them.

Jeff Sternal
Thanks for the info... I wasn't sure if it was possible/worthwhile... this is something that will most likely be shared, so i'm going to stay away from the viewstate.
Patrick
+1  A: 

You could also output the min and max as custom attributes of the textbox (and even namespace them if you want to retain xhtml validity); then you'd reference them as $thebox.attr('min') and $thebox.attr('max') or similar.

DDaviesBrackett
W/o having to do more than add an if statement, i would say that THIS was the easiest to implement w/o having to make major changes to the code... on render i just added additional attributes with the min and max and just threw in the jquery with the .attr.... its a bit more complicated since i'm using the sibling of 'this' for the buttons and pulling back the 'input' box... but its pretty straight forward once you get past that.
Patrick