I am passing in a value to the form that I ultimately assign to a textbox using javascript. The value is a calculation based on values from another page/form. However, the textbox is editable.
When reloading the form with the previo开发者_JAVA百科usly saved data, I don't want the value of the textbox to be overwritten with the pre-calculated value from another page if it was edited manually upon save.
The other stipulation is, the page/form that has the values that the calculation is created from, can be changed. Therefore, I would also want to check to see if the calculated value has changed since last load, and if so, load that over the manually entered value.
So... If textbox is blank populate with calculated value else if Manually entered value trumps calculated value else if calculated value has changed since last calculation, it trumps manually entered value.
The other concern with this all, is how do I determine if the user has typed in the textbox to determine if the value was manually entered?
You can accomplish this with a couple of session variables. The first session variable is the value of the TextBox. Since you have another page generating the value for you, you can't rely on the ViewState to store the value for you, it only work with PostBacks on the page.
The second session variable is a simple boolean value you can call IsCalculated. This can be set to true or false based on the situation at hand.
The final step is when to trigger the change of the IsCalculated value. You can easily wire up a TextChanged event to the TextBox using jQuery, or even standard Javascript. Unless you want to setup AJAX as well to set the Session variable, your best bet would be to have a hidden form field called IsCalculated as well that is modified by the TextChanged event.
When loading the "primary" page, you can then easily implement the rules listed above on which value needs to go into the textbox. If the value is then manually changed, the JavaScript event will trigger, and when you move on to the "calculation" page, you'll want to check the hidden form field and set the session variable of IsCalculated accordingly. You can do whatever calculations are needed, return to the "primary" page, and implement the display rules again accordingly.
Note: You mentioned in the question that the calculated value would be passed to the page, I'm assuming through the URL parameters, which is why you only need a single session variable tracking the value of the TextBox. If this is not the case, you may need a second session variable. One to track the TextBox value, and one to track the calculated value going in.
I would suggest storing all those values in hidden fields in your form. Once you have them on the page you can easily make any comparison using JavaScript.
精彩评论