ASP.NET web pages commonly display messages in response to user actions. For instance, a typical CRUD (Create, Read, Update, Delete) web page might display the message, “Record deleted” immediately after deleting a record and the message “Record updated” immediately after updating a record. Likewise, there would be messages displayed for inserting a new record and messages displayed in the event of an error. Such messages are typically displayed using a single Label Web control. This control may be located at the top of the page and have its
Text property initially set to empty string. Then, when particular events transpire, the Label’s
Text property is updated accordingly.
The problem with this approach is that the Label’s
Text property value is remembered across postbacks. Consequently, if a user deletes a record the “Record deleted” message is displayed. If the user next sorts or pages the CRUD grid or perform some other action that results in a postback, the Label control continues to display the message “Record deleted,” which is now outdated. What we want is to have the Label’s
Text property revert to an empty string on subsequent postbacks.
There are two simple and straightforward techniques at our disposal for implementing such functionality. This article examines these two approaches and includes a working demo available for download that highlights both approaches. Read on to learn more!