eBusiness Help
Try The Internet's Most powerful SEO Software For Free
Literally reveals the winning linking structures of any of your competitor's websites
Everything You Will Ever Need To Market A Website On The Internet
Increase Traffic to ANY Website - Whether it is an Enterprise Company, Small Business or Private Website
Manage Reciprocal Links
Create pages of exit links, reciprocal links with related sites, one-way links, a large themed directory or portal site, a news resource, advertisements structured as links, other exit resources, dictionaries, custom search engines, and more!

WebProWorld Dev Forum

PHP - Extreme Programming?
Can anyone tell me what does meant by "EXTREME PROGRAMMING IN PHP"? I think we should share our experience on this topic...
Click to read more...

HTML validation help
I've been working on my html validation, because I never really paid attention to it, until I started to use Firefox and Safari, and I really want to have a validated html code.
Click to read more...

PopUp windows for a PU blocker world?
I'm interested in strategies for most effectively using PopUp windows in today's PU blocked world - making PU windows with the least likelyhood of being blocked.
Click to read more...



Recent Articles

Industry Partners Build Password-Reset Solutions on Microsoft Speech Server 2004
Password resets are the second most common reason workers call help desks, accounting for about one in four help desk requests, according to the Gartner Group, an IT research company.

Navision Database access via C/ ODBC WebService/ ASP.NET Application
Navision together with Microsoft Great Plains, Axapta, Solomon, Microsoft CRM and Microsoft RMS are now supported by Microsoft Business Solutions.

Dozing Dogs ASP.NET CMS
ShawThing doing business as Dozing Dogs, released version 2.0 of their ASP.NET Content Management System (CMS).

A Detailed View of the Global.asax File
The global.asax file is the ASP.Net counterpart of the global.asa file used in traditional ASP Applications. This file is the placeholder for code to respond to application level events raised in ASP.Net or by HTTP modules.

Dominion Digital Senior Consultant Co-authors ASP.NET Cookbook
Dominion Digital announced that senior consultant Michael Kittel has co-authored a book titled ASP.NET Cookbook with former company consultant Geoffrey LeBlond.

Urchin Releases ASP Web Analytics Software
Urchin Software Corporation today released Urchin 6 On Demand, an ASP version of its popular web analytics and marketing intelligence software.

Go Daddy Hosting to Include ASP.NET Support
The Go Daddy Group has updated its web site hosting plans with more storage, more monthly data transfer and more email account flexibility while expanding its offerings to include ASP.NET support.

10 Basic Requirements in Choosing a Good ASP Hosting Account
In the world of internet application, solutions are greater than products. Individuals and companies are still investing in web technologies, but they are investing in solutions with demonstrated benefits. If you are looking for top-notch ASP hosting solutions that helps you to deliver quick productivity and profitability...

Creating an Online RSS News Aggregator with ASP.NET Part 4
Displaying the News Items for a Particular Syndication Feed The next task that faces us is creating the DisplayNewsItems.aspx Web page.

02.04.05


Using ASP.NET To Prompt A User To Save When Leaving A Page

By Scott Mitchell

Previously I wrote an article titled Prompting a User to Save When Leaving a Page, which looked at how to use the client-side onbeforeunload event to display a confirmation messagebox when a user attempted to leave a data-entry page after having modified the data's contents without explicitly saving the data.

To summarize last week's article, adding such a feature required the following steps:

1. Writing code that saves the initial values of the page's input form fields in a client-side array.

2. Creating an event handler for the onbeforeunload event that checks to see if the input form fields' values differ from those in the array. If they do, then a string is returned, prompting the user if they really want to leave. Otherwise, no string is returned, and the user can leave the page as they normally would (i.e., without being prompted).

3. Finally, for buttons or other HTML elements that could cause the user to leave the page but should not require that the user be prompted, a client-side onclick event handler was used to set a flag that indicated that the onbeforeunload event handler didn't need to check for changes. This is typically used with a "Save" button that, when clicked, causes a postback, but whose posting back should not prompt the user that they are about to leave the page.

LinksManager manages reciprocal links, and helps increase website traffic through linking with other like-minded quality sites -> more info

As the article examined, these steps could be accomplished by adding two client-side <script> blocks and client-side onclick events where needed. While adding this script code is not terribly difficult, I find it simpler to move this logic to server-side methods that will inject the appropriate client-side script for us. In this article we'll examine how to extend the base Page class, adding a couple of methods that will allow for a user to be prompted when they leave the page without saving without the page developer having to write a single line of client-side script code. (If you've yet to read Prompting a User to Save When Leaving a Page, please be sure to do so before continuing on.)

A High-Level Look at What We Want to Accomplish

The code-behind class for any ASP.NET Web page is derived, either directly or indirectly, from the Page class in the System.Web.UI namespace. The Page class contains the base functionality that all ASP.NET Web pages must provide. For example, the Page class includes properties like IsValid and IsPostBack, and events like Load (which triggers the Page_Load event handler to execute). If there is some functionality that you know you will need in a number of pages, you can provide this functionality by creating a custom class that derives from the Page class, and then have your ASP.NET Web pages' code-behind classes derive from this custom class (rather than from the Page class directly).

In this article we'll create such a custom class that extends the Page class. Our extended Page class will contain methods that we can call that will inject the appropriate client-side script so that if a user attempts to leave the page after making changes without saving, a confirmation will be displayed. Specifically, there will be two methods:

  • MonitorChanges(webControl) - this method accepts a Web control (such as a TextBox, CheckBox, DropDownList, etc.) and adds the necessary client-side script to monitor this control's value. That is, if this control's value is changed and then the user attempts to leave the page without saving, they'll be prompted with a warning.

  • BypassModifiedMethod(webControl) - this method is used to indicate that a particular Web control should not cause the confirmation to be displayed. This will typically be used on "Save" Buttons, LinkButtons - namely on Web controls that might cause a postback but, even if changes have been made, should not display the confirmation.

    In order to squirt out the correct client-side script, these methods will utilize three methods from the Page class: RegisterClientScriptBlock(), RegisterStartupScript(), and RegisterArrayDeclaration(). These three server-side methods add client-side code to the ASP.NET page's rendered markup. Let's take a brief moment to examine these three methods.

    Working with Client-Side Script Code in Server-Side Code

    Working on Web applications requires a keen understanding of the logical, physical, and temporal differences. These differences were more apparent in classic ASP, but ASP.NET and its Web Forms paradigm effectively blurs the distinction between the client and server. Regardless, the distinction still very much exists and it is important to be familiar with the separation.

    There are often times when we might want to inject client-side script from a server-side method. To facilitate this the Page class provides a number of methods. To add a block of client-side script code, use either RegisterClientScriptBlock(key, script) or RegisterStartupScript(key, script). These methods both accept two string values as input: a key and a script value. The key uniquely identifies the script block being injected, while the script value contains the actual client-side script to inject. (Note that the script input parameter must include the precise markup to inject, including the <script> tag itself.)


    The main difference between these two methods is the location in the markup where the controls emits its script. Both methods inject their script content inside the <form>, but RegisterClientScriptBlock(key, script) adds it before the Web controls within the form, whereas RegisterStartupScript(key, script) adds the script after the Web control markup.

    The other Page class that we'll need to utilize is the RegisterArrayDeclaration(arrayName, arrayValue). This method creates a client-side array with the values specified. To create an array named foo with values 1 through n you'd call the RegisterArrayDeclaration(arrayName, arrayValue) method n times, like so:

    Page.RegisterArrayDeclaration("foo", "1");
    Page.RegisterArrayDeclaration("foo", "2");
    ...
    Page.RegisterArrayDeclaration("foo", "n");


    A thorough discussion of the client-side injection methods in the Page class is a bit beyond the scope of this article. For more information, utilize the following two articles of mine: Working with Client-Side Script and Injecting Client-Side Script from an ASP.NET Server Control. (The first article also discusses in more detail the process of extending the functionality of the Page by creating a custom, derived class, and having ASP.NET pages' code-behind classes deriving from this custom class.)

    Read the Rest of the Article.


    About the Author:
    Scott Mitchell, author of five ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies for the past five years. An active member in the ASP and ASP.NET community, Scott is passionate about ASP and ASP.NET and enjoys helping others learn more about these exciting technologies. For more on the DataGrid, DataList, and Repeater controls, check out Scott's book ASP.NET Data Web Controls Kick Start (ISBN: 0672325012). Read his blog at : http://scottonwriting.net

  • About WebProASP
    WebProASP is a collection of up to date tutorials and insightful articles designed to help ASP users of any skill level implement successful ASP systems and practices. ASP Strategies and Tactics for Business

    WebProASP is brought to you by:

    SecurityConfig.com NetworkingFiles.com
    NetworkNewz.com WebProASP.com
    DatabaseProNews.com SQlProNews.com
    ITcertificationNews.com SysAdminNews.com
    WebProASP.com WirelessProNews.com
    CProgrammingTrends.com ITManagementNews.com


    -- WebProAsp is an iEntry, Inc. publication --
    iEntry, Inc. 880 Corporate Drive, Lexington, KY 40503
    2005 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

    archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

    ASP Strategies and Tactics for Business WebProASP News Archives About Us Feedback WebProASP Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact