views:

361

answers:

1

I am working on a legacy web application written in VB.NET for ASP.NET 1.1. One particular page has a form with a number of fields. In response to a drop down box changing value, I am clearing a number of fields, resetting a number of drop down boxes to the first option, and setting them all to "disabled" in the UI. To do this, I'm using jQuery. I add a css class to all of these fields, and then my jQuery selector is something like the following: $("*.my-css-class"). Here's some sample code to explain.

var fields = $("*.fields");
if( some_condition )
{
fields.val("");
fields.attr("selectedIndex", 0);
fields.attr("disabled", "disabled");
}

The UI updates as expected in response to the above js code, but when I post back the page in response to a button click, the original values still persist on the server side related to these controls. For instance, txtSomething is one of the fields with a css class "fields" (so it will get selected by the above jQuery selector). The user types "1234" in this text box and submits the form. Either the same page is posted back to itself retaining its values, or I return to this page and prepopulate the values on the server side (for example, the user clicks an Edit button on a summary page), so the control txtSomething is initialized on the client with the value "1234". My jQuery code clears the value as far as the user sees it in the UI, and then the user clicks a submit button. If I interrogate the value with a jQuery selector, the value of this field is an empty string. When the page is posted back and I'm stepping through the code (or doing something with the value of this control), it is still "1234".

A very important point to make is that these values are sent back to the browser after being submitted once. So, picture a form being submitted, or any case where these values are bound or set on the server side and outputted to the browser pre-populated (as opposed to being output to the browser with default or empty values). If I load the page as default (empty text boxes), enter some text, and then trigger the js function to clear these fields, the value I typed never makes it to the server.

+1  A: 

why do you need to disable those fields? Disabling controls can make them not post values to the server ... at least that is what happens when an asp.net control is disabled server side.

Update 1: couldn't take having the doubt if it was only server side, so I looked it up :) http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1 ... "In this example, the INPUT element is disabled. Therefore, it cannot receive user input nor will its value be submitted with the form.", so I was right, even when disabling it client side it won't post the value

eglasius
I'm not disabling them on the server side. I'm disabling them on the client so that the user cannot select or type a value into them.
Rich
Excellent...thanks. I used "readonly" instead of "disabled" and the values are now sent to the server. My only issue now is that "readonly" doesn't work on the drop down lists, so I'll either find an equivalent attr or do some ugly manual stuff on the server for just the selects. Thx again
Rich