|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionValidation is an extremely useful feature in ASP.NET. The powerful validation controls provided by Microsoft allow developers to easily validate form data with a minimal amount of code. More specifically, the
The "Ugly" ValidationSummary ControlSo, you'd like to use validation on your ASP.NET page. You create your form, throw on a few To your dismay, however, you find that the
What if there was a way to take all the messages from your validation controls, and pop them up into your own custom error window? It turns out, there are several ways to do this. Sure, you could set the
Rather than rewriting the Modifying WebUIValidation.jsFirst of all, you'll need to find the WebUIValidation.js file. In ASP.NET 1.1, this should be located in the X:\Inetpub\wwwroot\aspnet_client\system_web\1_1_4322 directory. If you are running another version of ASP.NET, you may have to search for the file. Once found, open the file in your favorite editor. Before we begin, we need to add a function in the file that will URL encode a string of text. This will ensure that no problems occur when we pass messages in the Query String. This function was borrowed from here. /* URL encodes a string, making it suitable to pass in a query string.
Borrowed from http://www.rgagnon.com/jsdetails/js-0096.html. */
function URLencode(sStr) {
return escape(sStr).replace(/\+/g,
'%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}
We're going to concern ourselves only with the if (summary.showmessagebox == "True") {
Right before that line is as good as any place to insert our code. // If a custom popup is desired, and a URL was specified ...
if ((summary.showcustompopup == "True") &&
(typeof(summary.customurl) == "string"))
{
var width;
var height;
// Width, height are passed in the customwidth and customheight properties.
width = (typeof(summary.customwidth == "int") ? summary.customwidth : "200");
height = (typeof(summary.customheight == "int") ? summary.customheight : "200");
// The query string that will be passed to the custom URL.
s = "";
// Iterate through each error, adding them to s, and separating by a '|'.
for (i=0; i<Page_Validators.length; i++)
if (!Page_Validators[i].isvalid &&
typeof(Page_Validators[i].errormessage) == "string")
s += (Page_Validators[i].errormessage) +
((i == (Page_Validators.length - 1)) ? "" : "|");
// Build the query string.
s = summary.customurl + "?err=" + URLencode(s);
// If an icon was specified, add it to the query string.
if (typeof(summary.icon) == "string")
s += "&icon=" + URLencode(summary.icon);
// If a header was specified, add it to the query string.
if (typeof(summary.customheader) == "string")
s += "&header=" + URLencode(summary.customheader);
// If a page title was specified, add it to the query string
if (typeof(summary.customtitle) == "string")
s += "&title=" + URLencode(summary.customtitle);
/* Check if the caller requested a modal dialog,
* and use the appropriate method to open the window. Note that
* modal dialogs currently only work in Internet Explorer 5.0+ */
if (summary.modal == "True")
window.showModalDialog(s,"","dialogWidth:" + width +
"px;dialogHeight:" + height + "px;status:no;");
else
window.open(s, null, "height=" + height + ",width=" +
width + ",status=no,toolbar=no,menubar=no,location=no");
}
Creating The Validated FormSave the WebUIValidation.js and open Visual Studio .NET. Create a new ASP.NET Web Application, and add a few Finally, add a
A sample customized <asp:ValidationSummary class=code id=vsmSummary ForeColor="DarkRed" runat="server" showcustompopup="True" customurl="Msgbox.aspx" customwidth="225" customheight="250" customheader="Attention!" customtitle="Error on Form" modal="True" icon="warning" ShowSummary="False"/> The last thing that you need to do is to create the popup page -- the page that will popup and display the error message to the user. This is simply a matter of reading The above
The Finished Product - Advantages and DisadvantagesSo, now we have a customizable error message that does not alter or use up screen real estate on our form as the Another perhaps more common problem is that of popup blockers. If the user has such a feature enabled, they may not see your popup. Once again, however, the asterisks displayed will help to alleviate this problem. Finally, if you attempt to display a modal popup, only users of Internet Explorer 5.0 or higher will receive it. So there are both advantages and disadvantages to this method. While it may not be the best option for ensuring cross-browser compatibility, it can be a nice addition to a professional web site, allowing you to customize error messages to your site's theme, or perhaps display customized help based on the form fields that the user populated incorrectly.
|
||||||||||||||||||||||||||||||||||||||||||||||||