If you have web part that has code behind to do something (like add data to a list) and it has an exception during processing you need a way to display that error.
Here is a generic method I use to display the error.
I put this in all my catches. It will add a new literal control with the error.
/// <summary> /// Clear all child controls and add an error message for display. /// </summary> /// <param name="ex"></param> private void HandleException(Exception ex) { this._error = true; this.Controls.Clear(); this.Controls.Add(new LiteralControl(ex.Message)); }
Advertisements
Hello,
Nice trick right there. I remember when I first started in SharePoint. I did get a lot of errors on WebPart which leads me to either WebPart Maintenance Page or just complete Correlation ID error. BTW, do you think of creating a virtual webpart class that contains all the default handling like this and go from there ? May be these handlers could be virtual protected so if we need special exception handling for a certain webpart we can just override the base method ?
I’m not trying to be smart ; ) but it is just my thought.