Scott on Writing

Musings on technical writing...

Update to My MSDN Article "Working with Client-Side Script"

This is an unofficial update to a recent MSDN Online article I wrote titled Working with Client-Side Script.  In that article I examined a number of common client-side tasks, and looked at how to accomplish them easily with ASP.NET by extending the Page class.  Specifically, I looked at how to do things like: display an alert dialog box; how to display a confirm dialog box when deleting or performing other “Are you sure you really, really want to do this?”-type of activities; how to display a popup window; and how to alert the user if they're about to leave a page from which they've made changes but have yet to save those changes.

In my article I accomplished the last common task by adding methods to the extended Page class that allowed a page developer to easily add a client-side onchange event to the data-entry Web controls that would set a flag indicating they had been modified.  Then, when leaving the page through a button click, hyperlink, etc., the user would be prompted with a standard confirm dialog asking them if they were sure they wanted to leave the page (assuming that the dirty flag was true).

This approach had two drawbacks:

  1. It flagged the data as dirty from a simple change.  That is, if a user changed a textbox value from “Scott“ to “Jisun“ and then back to “Scott,“ when leaving the page they'd be warned that they were leaving without saving.  This warning would happen because the data had been altered at least once, even though it had been changed back to its original value.
  2. If the user left the page through some other means (i.e., not necessarily by clicking on some navigational Web control), then they could leave the page without the warning.  Ideally the user would receive a warning if they closed their browser, typed in another URL in their browser's Address bar, or even clicked a link in, say, Outlook, that redirected that browser instance to another page.

Last week on 4Guys I wrote an article - Prompting a User to Save When Leaving a Page - that looked at how to use the client-side onbeforeunload event and some clever JavaScript.  The onbeforeunload event fires whenever the document is unloading, and gives the developer a chance to prompt the user.  The user can choose to either continue with the unloading of the document, or can stop it outright.  This works even if they attempt to close the browser window.  (One caveat is that there's not standard browser support for onbeforeunload.  IE supports it, as does FireFox 0.9+.)

Today I wrote an article titled Using ASP.NET to Prompt a User to Save When Leaving a Page.  This article looks at how to extend the Page class to include methods to easily add this onbeforeunload support to your ASP.NET data-entry forms.  This article serves, in a roundabout way, as an update for the technique examined in the MSDN article.  (Although the MSDN article's approach is more compatible with non-standard browsers.)

posted on Tuesday, October 12, 2004 5:27 PM

Feedback

# re: Update to My MSDN Article "Working with Client-Side Script" 11/5/2004 10:42 AM William Groombridge

Sounds complex, why not just:

__btnReturn.Attributes["OnClick"] = "javascript:return confirm('Are you sure you do not wish to place an order at this time?');" ;

# re: Update to My MSDN Article "Working with Client-Side Script" 11/5/2004 11:01 AM Scott Mitchell

William, the idea is to prompt the user if they *change* any value in any of the form fields on the page. That is, say the user changes the value in a textbox and then closes their browser without having clicked the "Save" button. Your technique will just let them close the browser, thereby losing the chnages they made. My technique, on the other hand, will display a dialog box, warning them that they are about to leave the page without having saved their changes.

See the difference?

# re: Update to My MSDN Article "Working with Client-Side Script" 1/18/2005 9:04 PM Vishal

Hello Scott,

We are developing a web application using asp.net. For some functionality we have used your code to check the changes done in any control on the screen (CLientSidePage Class).That prompts the user to save when leaving the page.

Things are working fine as the message pops up when user tries to exit from the screen after doing some changes.
But we are facing a problem in implenting the code in our application wherever the IsPostBack property is True for the control. For example message pops up even on the text change event of a control like textbox.
Could you provide us with some solution in this.

# re: Update to My MSDN Article "Working with Client-Side Script" 4/25/2005 1:36 AM Martin Nyborg

The code works great, thanks
But there is a problem with the skmMenu.

When I have a page with the skmMenu and I activate a menu item I get the prompt if I wish to leave the page that’s fine, but if I press cancel I got an error in this skmMenu script:

onclick="javascript:skm_closeSubMenus(document.getElementById('PageHeader1_Menu1_skmMenu'));location.href='/ResPlan/Employee/EmployeeSearch.aspx?SearchFictiveOnly=1';" onmouseover="javascript:skm_mousedOverMenu('PageHeader1_Menu1_skmMenu',this, document.getElementById('PageHeader1_Menu1_skmMenu'), false, '');skm_shimSetVisibility(true,'PageHeader1_Menu1_skmMenu-menuItem005-subMenu');this.className='mouseover';" onmouseout="javascript:skm_mousedOutMenu('PageHeader1_Menu1_skmMenu', this, '');this.className='menuitem';">Fictive Employees</td><td class="menuitem" id="PageHeader1_Menu1_skmMenu-menuItem006"

Med venlig hilsen / Best Regards

Martin Herløv Nyborg

Direkte tlf: 4598 6815
mvn@ramboll.dk

Rambøll Danmark A/S
Bredevej 2
DK-2830 Virum
www.ramboll.dk

# re: Update to My MSDN Article "Working with Client-Side Script" 12/8/2005 8:02 PM Heath Aldrich

I notice you have the articles on confirming a page before leaving... Have you put anything together that combines both the "onclick" warning technique from MSDN and the beforeunload technique?

I am trying to combine the two and was curious if you had already worked it out.

Thanks!
BTW, Great stuff... I learn a lot from your discussions.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles