How to display temporary error messages in ASP.NET
I was writing some code and wanted to display an error message to the user when something didn’t work right.
Naturally, I decided to use an <asp:Label/> control to do this, and just set its .Text property to something like “Oops! you fudged up!” However, I found out that if you do this, the error message persists across subsequent post-backs. This is a problem because you don’t want the error message to just stick around forever; you just want it to display once and then go away when the user posts the page again.
The solution I’m using is to disable the ViewState for the <asp:Label/> control. This tells ASP.NET not to remember anything about the particular control between post-backs; everything’s reset back to default on every page load.
Here’s how you disable the ViewState for a label:
<asp:Label runat=”server” ID=”MyErrorLabel” EnableViewState=”false” />
Blogged with Flock
Tags: asp.net