I have some JavaScript to toggle a dropdownlists in a ASP.NET page, which gets called when I click a button. I have like 4-5 dropdownlist/toggle button pairs. Each toggle button toggles the enable/disable property on the associated dropdownlist.
I save the disabled property value of a hidden field to track the disabled state of a button between postbacks, with the following javascript
function disableButton(dropdownID)
{
var element = document.getElementById(dropdownID); // get the DOM element
var trackingField = document.getElementById("_tracking" + dropdownID);
if (element) {
trackingField.value = element.disabled;
//sending the element.disabled instead of "!element.disabled" since we are
//setting dropdownlist.enabled property so is already negated for us
element.disabled = !element.disabled; // invert the boolean attribute
}
return false; // prevent default action
}
HTML
<input type="hidden" name="_trackingdropdownlist" id="_trackingdropdownlist" value="true" />
Code
if (Request.Params[_trackingdropdownlist] != null)
{
bool val = true;
bool.TryParse(Request.Params[_trackingdropdownlist], out val);
dropdownlist.Enabled = val;
}
So the state of the dropdownlist are maintained on the first postback round trip but after that all the dropdownlist get enabled. What is going wrong here?
*Note: The default enabled property value on these dropdownlist are false.
your hidden elements are not server elements, and their values are being lost across postbacks. when the page reloads, the hidden elements are recreated with the value “true” as you specify. when you check the request value at the second postback, all the hidden elements that haven’t been modified between 1st and 2nd postbacks will contain true.
make them server elements so any changes you make are maintained in viewstate.