Client side/server side event race.

 Hi.  I've got a page that

1) Takes some text fields as input

2) Does some math calculations on the input (server side via a "calculate" button click)

3) Then opens a new window with some detailed information (client side, via a "view report" button click)

The server needs to "calculate" before the user can click "view report" so I start with "view report" disabled.  I've got input validation on the "calculate" click, and at the end, I enable the "view report" button.  Everything is working well with one exception: If a user were to enter data, click calculate, then change the input and click "view reports" it's possible they would mistake the results from the previous "calculate" click, which is based on the old input.

 I added to the text boxes an event that disables the button from their "TextChanged" event.  This works well, except when an input is changed, and then the "view report" button is clicked.

The button's onclientclick event (client side) executes before the TextBox's TextChanged event (server side), displaying the stale report.

I would like to be able to replace the server side "TextChanged" event for a client side solution, that way it will fire before the button's "onclientclick."

Any help would be appreciated.

9/25/2007 11:51:34 PM
📃 24353 articles.

💬 6 Replies

Why don't you make a function and add the textbox change event code in it. And in button Click event code call that function before setting the reports. So it will always perform the text change calculations before setting report.

I compete with myself to motivate me!!

Do not forget to mark posts, that help you, as "Answer".
9/26/2007 1:53:02 AM

I guess it depends on what you mean by "setting" the report.

If by setting, you mean creating, then that's what I'm currently doing.

When the "Calculate" button is clicked:
1) Input is validated.
2) Calculations are done.
3) The calculated data is put into a string (the report).
4) The report button is enabled.

This still leaves the possibility for modifying the input fields after the report is created, but before the report is shown.

If by setting, you mean writing the report to the browser window (displaying it to the screen), there are a couple of problems with that.
1) The report generation is server side.  Displaying the report is client side.  I don't know how to call a server side method calculate from a client side script.
2) If I did recalculate in this way I would (by my estimation) end up transmitting an extra 100k and holding 200-225k in server memory *per button click* until the .NET garbage collection decides it's good and ready to come and free up that abandoned memory.  If I could clear up my own memory immediately, I would do this without hesitation, but I can't and Server 2000 Web Edition has a maximum of two gigs of ram.  Two gigs of ram coupled with moderate to high traffic and uncertain memory usage keeps me awake at night.

In my opinion it's too expensive to recalculate like that.

9/26/2007 5:52:27 PM

I've tried adding these to the Page_Load event:

daysBox.Attributes["onchange"] = "javascript:document.getElementById(\"ctl00_MainPlaceHolder_viewReport\").enabled = false;";
daysBox.Attributes["onchange"] = "javascript:document.getElementById(\"" + viewReport.ClientID + "\").enabled = false;";
It doesn't work.  Any ideas why? (In c# the button name is viewReport, but I'm using a master page.  That's why I'm using ctl00_MainPlaceHolder_viewReport instead.)
9/26/2007 6:36:44 PM

The javascript onchange event does not fire until after focus has gone to another input element.  I think you are looking for "onkeyup".  Also, remove the "javascript:" becuase it is not needed.  And the most important of all, there is no enabled property in Javascript.  It's backwards, you need to use "disabled" and set it to true.

Hope this helps!  Please mark the helpful post(s) as Answer.

Josh Stodola ← Come check out my blog!
9/26/2007 7:06:15 PM

Thanks, making those changes, I'm getting the button to disable... but I'm thinking "onkeyup" might be a bit overzealous.  IT disables the button even if the user tabs through the fields without making any changes.

I don't really see any events that are better suited to this than "onchange"  It does end up firing right before the "onclientclick" event of the button, so I guess the "onchange" does what I want it to do.


Thanks for your help! Smile 

9/26/2007 7:15:35 PM

OK, no problem!  I was thinking you were looking for something like onkeyup but I didnt think about the tab issue.  As long as onchange does what you need it to then we're all good.  Glad you got your problem solved!

Best regards...

Josh Stodola ← Come check out my blog!
9/26/2007 7:20:50 PM