The ASP.NET validation summary is great for dis…

The ASP.NET validation summary is great for displaying all the errors in the page. It would be nice though to be able to use a validation summary to display errors that occur on the server side.

A typical scenario is when you have a page that does a search and then displays the results of the search. For the case where no results are found, it would be nice to be able to display the results in our custom validation summary control. It takes away the need to handle the display of the error message in a validation summary control.

This is what you can do about it – implement the IValidator class


public
class CustomErrorMessage:IValidator
    {
        privatestring message;
        public CustomErrorMessage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IValidator Members

        publicvoid Validate()
        {
            // TODO: Add CustomErrorMessage.Validate implementation
        }

        publicbool IsValid
        {
            get
            {
                returnfalse;
            }
            set
            {
                
            }
        }

        publicstring ErrorMessage
        {
            get
            {
                
                return message;
            }
            set
            {
                this.message = value;
            }
        }

        #endregion
    }

And here’s code to use the validator at runtime in response to a server side error.

CustomErrorMessage msg = new CustomErrorMessage();
msg.ErrorMessage = “No rows found”;
ServerErrors.Visible =true;
ValidationSummary1.Visible = false;
Page.Validators.Add(msg);

Page.Validate();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s