English 中文(简体)
Substitute User Controls on Failure
原标题:

Recently, I had a user control I was developing throw an exception. I know what caused the exception, but this issue got me thinking. If I have a user control throw an exception for whatever reason and I wish to replace that usercontrol with something else (e.g. an error saying, "Sorry, this part of the page broke.") and perhaps log the error, what would be a good way to do it that could be done independently of what the user control is or does (i.e. I m not saying what the user control does/is, because I want an answer where that is irrelevant).

Code sample:

<asp:TableRow VerticalAlign="Top" HorizontalAlign="Left">
    <asp:TableCell>
        <UR:MyUserControl ID="MyUserControl3" runat="server" FormatString="<%$ AppSettings:RVUC %>"
            ConnectionString="<%$ ConnectionStrings:WPDBC %>" Title="CO" />
    </asp:TableCell>
    <asp:TableCell>
        <UR:MyUserControl ID="MyUserControl4" runat="server" FormatString="<%$ AppSettings:RVUA %>"
            ConnectionString="<%$ ConnectionStrings:WPDBA %>" Title="IEAO" />
    </asp:TableCell>
</asp:TableRow>
问题回答

There s two things at work here:

  1. Catching the error
  2. Swapping in an "error" control

Catching the error is pretty straightfoward. It comes down to try...catch around the code that could conceivably throw an error.

As for swapping in the error control (in the catch block), there s a couple ways to do it.

  1. You could have a hidden placeholder in your control that you just display that says, "there was an error." So, you re not removing the bad control, just replacing what it says.
  2. Each control has a "Parent" which has a "Controls" collection (which the current control will be part of). So, you could iterate the Parent.Controls collection to find the current control, then do "Parent.AddAt(index, myErrorControl)" to insert the error control, then remove the current control (or just set "Visible = false" on it).

General ID: for ASP.NET it might work (not sure about ASP)

Before rendering each control - record it s id.

String CtrlID;

before each control:

CtrlID = control.name

put everything in try/catch (or do it to each control)

catch (Exception EX)
{
  hide(ctrlId)  (either set visible=false, or whatever suits you)
  show(error message)

}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签