February 27, 2021
Hot Topics:

Messaging with Modal Windows in ASP.NET

  • By Michael Klaene
  • Send Email »
  • More Articles »

That's it. The messaging framework is now in place. I have created a simple FormView in Default.aspx that collects first and last name for an individual and attempts to save the record to the database. However, before any data saves can occur, you need to perform some validation. You will add any errors to your collection and let the Master Page display them. Because you've really created an 'informational' framework that is capable of displaying more than just errors, you can provide users with warning and informational messages as well. The following event handler in Default.aspx.cs fires when the 'Save' button is pressed. You check user input, potentially saving that record to the database, and add it to your Message collection as necessary:

protected void btnSave_OnClick(object sender, EventArgs e)
   string firstName = ((TextBox)this.fvEmployeeData.FindControl
   string lastName =  ((TextBox)this.fvEmployeeData.FindControl

   MessageProvider msgProvider = MessageProvider.getInstance();
   bool hasErrors = false;

   if (lastName == null || lastName.Trim().Equals(""))
        msgProvider.Messages.Add(new Message("You must enter a
           last name.", Message.MsgType.ERROR));
        hasErrors = true;

   //If okay, submit...
   if (!hasErrors)

      if (firstName == null || firstName.Trim().Equals(""))
            new Message("No first name was entered. Please
               update as soon as possible.",

         msgProvider.Messages.Add(new Message("Record saved.",
      catch (Exception ex)
         msgProvider.Messages.Add(new Message("Unexpected error
            occurred: " + ex.Message, Message.MsgType.ERROR));

In the preceeding code, you consider a last name to be required data. If it is not provided, an error message is created. If a first name is not entered, you allow the save to proceed but warn the user to enter one at a future date. Upon a successful save, you provided user feedback as well. There is no limit on the number of messages that can be displayed and different types of messages may be displayed simultaneously. The contents of the IList<Message> are displayed after each postback and then the messages are removed.

Click here for a larger image.

Error when user fails to enter last name

Click here for a larger image.

'Warning' message along with an 'Informational' message when a user saves without first name

Click here for a larger image.

Message upon successful save


With this system in place, you can enhance your ASP.NET applications with modal windows that provider user feedback in a clean and efficient manner.

About the Author

Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over nine years in IT, specializing in J2EE, .NET, and Oracle analysis and development.

Page 2 of 2

This article was originally published on September 12, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date