TemplateControl.OnError(EventArgs) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 Error 事件。
protected:
virtual void OnError(EventArgs ^ e);
protected virtual void OnError (EventArgs e);
abstract member OnError : EventArgs -> unit
override this.OnError : EventArgs -> unit
Protected Overridable Sub OnError (e As EventArgs)
参数
示例
下面的代码示例演示了使用 方法的 OnError 可能方法。 它创建一个字符串,其中包含一些无效的参数,这会导致异常。 TemplateControl由于 类是抽象类,因此此代码示例是使用 Page 派生自 TemplateControl 类的 类编写的。
<%@ Page language="VB" %>
<html>
<head>
<script language="VB" runat="server">
Sub Page_Load(sender as object, e as System.EventArgs)
AddHandler Page.Error, AddressOf Error_Handler
try
Dim myString as string = "Hello"
'An exception is raised because the length of 'myString' is less than '6'.
Dim mySubString as string= myString.Substring(0,6)
catch e1 As Exception
OnError (EventArgs.Empty)
End try
End Sub
Sub Error_Handler(sender as object,e as System.EventArgs)
Response.Write("<br><b>An Error was raised</b>")
End Sub
</script>
</head>
</html>
<%@ Page language="c#" %>
<html>
<head>
<script language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
Page.Error += new System.EventHandler(Error_Handler);
try
{
string myString = "Hello";
// An exception is raised because the length of 'myString' is less than '6'.
string mySubString = myString.Substring(0,6);
}
catch(Exception)
{
OnError(EventArgs.Empty);
}
}
void Error_Handler(object sender,System.EventArgs e)
{
Response.Write("<br><b>An Error was raised</b>");
}
</script>
</head>
</html>