Catching unhandled exceptions in ASP.NET UserControls

前端 未结 7 1985
猫巷女王i
猫巷女王i 2021-01-06 00:58

I\'m dynamically loading user controls adding them to the Controls collection of the web form.

I\'d like to hide user controls if they cause a unhandled exception wh

7条回答
  •  萌比男神i
    2021-01-06 01:39

    Depending on where your errors are occurring you can do something like...

    public abstract class SilentErrorControl : UserControl
    {
        protected override void Render( HtmlTextWriter writer )
        {
            //call the base's render method, but with a try catch
            try { base.Render( writer ); }
            catch ( Exception ex ) { /*do nothing*/ }
        }
    }
    

    Then inherit SilentErrorControl instead of UserControl.

提交回复
热议问题