我有一个放置在用户控件(ascx)上的自定义控件。在加载自定义控件时发生错误。我想在用户控件(ascx)中“捕捉”这个错误。我怎么能这样做?
默认aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title></head>
<body><form id="form1" runat="server">
<asp:Label runat="server" ID="test" Text="hello world"></asp:Label>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</form></body></html>
WebUserControl1.ascx:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebApplication1.WebUserControl1" %>
<%@ Register tagPrefix="test" namespace="WebApplication1" assembly="WebApplication1" %>
<test:MyControl runat="server" ID="test2" Text="test"></test:MyControl>
WebUserControl1.ascx.vb:
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Try
'Doesn't work!
MyBase.OnLoad(e)
Catch ex As Exception
handleException(ex)
End Try
End Sub
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
'Doesn't work!
handleException(New Exception())
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub handleException(ByVal ex As Exception)
Me.Controls.Add(New LiteralControl("ex.tostring"))
End Sub
End Class
最后但同样重要的是:我的自定义控件:
我的控制.vb:
Public Class MyControl
Inherits System.Web.UI.WebControls.Label
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Throw New Exception("")
MyBase.OnLoad(e)
End Sub
End Class
我希望能够在 *.ascx 文件中捕捉到这个错误,这似乎是不可能的?或者是吗?