views:

2628

answers:

2

We have a page that contains a ListBox that is disabled (in the codebehind). This ListBox can be enabeled by checking a checkbox on the page; it gets re-enabled with javascript using JQuery.

So, the problem is when the control is first disabled then re-enabled on the page, it does not have a selected item even if one is selected.

The same code works fine if the ListBox was never disabled in the codebehind.

Is there any way to re-enable the ListBox, using JQuery, so the selected values will be posted to the server?


this is a .net 2.0 project using VS 2005 and no .NET MVC

+1  A: 

You will need to come up with a solution where the ListBox is always server-side enabled and visible. So a jQuery-only solution to disabling a select. I would add a hidden field, have it store a switch for whether or not the select should be enabled. This will allow your jQuery code to report to the server whether or not the control was enabled at the time of postback.

You would obviously have to write jQuery code to tie the hidden field and select together and manage the disabled/enabled state.

Edit: Now that I think about it. You could get the value of the select from the Request.Form collection and assigning the value to your ListBox during Page_Init. You would lose any ListBox.*Changed events.

Ken Browning
I thought of this, and we have a way to know if it should be enabled or disabled (the checkbox status) but the only problem is this is all also inside an update panel so it's difficult to disable on document ready because it's always being rewritten.
John Boker
Ahhh, I'm afraid I'm unfamiliar with UpdatePanels. But I edited to add another approach
Ken Browning
grabbing the value from the Request.Form worked perfectly.
John Boker
+1  A: 

A work around for this might be to get the value on postback via Request.Form[listbox.ClientID]. Its not as "nice" as using the ViewState but it should work.

iZ