 | - for a FREE 30-Day Trial |
WebProWorld Dev Forum | Simple JS "How To" question I think this question should be a simple one. Here is the scenario: I have 2 dropdown options list & one text field. What needs to happen is, when a user selects Yes or No from #1, #2 value needs to be "441".
XML 2 mail How to send parsed xml news to e-mail I am searching for it few days I try to explain easy ... let's imagine that we have public_html/latestnews directory and there are file news.php that parse my files
IE sudden login problems on PHP built site A few weeks ago, some of our members reported not being able to login with IE. I have XP and get automatic updates. I found that I now can not login using IE, but can login to members section using Netscape. The site is built in PHP and the site files have not changed.
|
 |
| Recent Articles | Microsoft's Key Windows Architect Defects To Google According to Microsoft Watch, Mark Lucovsky, one of Microsoft's key Windows architects, has defected to Google.
Simple Solution for PHP Includes I have recently created my first Php program. I wanted to share with others some of the problems that I encountered, and how I finally overcame these obstacles.
Password Protection with PHP, MySQL, and Session Variables One of the great promises that actually came true when our Internet-enabled world reached the twenty-first century is efficient customer-to-business interaction.
Intranet Portal Project - RAD or Waterfall? In this short article, David Viney examines whether Rapid Application Development (RAD) or Waterfall development methodologies should be used during Intranet Portal projects.
Bill Gates Talks Office Microsoft Chairman Bill Gates spoke at the Office Developer Conference yesterday held at Microsoft's Redmond, Washington headquarters.
Using ASP.NET to Prompt a User to Save When Leaving a Page 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...
Using Win32 Calling Conventions When writing code for the Win32 platform, most developers don't pay attention to selecting a "calling convention", and in most cases it doesn't really matter much. But as systems get larger and split into more modules (especially when third-party modules are to be included)
Oracle Unleashes Army of Developers Oracle CEO Larry Ellison announced the release by 2008 of "Project Fusion," the company's next-generation information- oriented application architecture and application set.
Using PHP CURL Library to Scrape the Internet Have you ever though how much information is there in DMOZ? Your entire life won't be enough to collect and sort it.
PHP On-The-Fly! PHP can be used for a lot of different things, and is one of the most powerful scripting languages available on the web. Not to mention it's extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to update pages in real-time
Sun Trying Something New ... Like Giving Away An Operating System Sun Microsystems is trying a new marketing strategy, giving away its new Solaris 10 operating system for free. Hewlett Packard sells a printer at a low price and makes a lot of money on printer cartridges. |
|
03.09.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.
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.
- for a FREE 30-Day Trial of vmware's Workstation 4.5 |
|
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 |
|