How do you handle the Web User Control event? I notice my custom web user control have a event call OnError but it never fire when i tweak the control to fail. The control is basically a custom gridview control. I search for web user control event handling over the net but i haven't find a article that address what i looking for. Can someone do a quick explanation or point me to the right direction?
你如何处理Web用户控制事件?我注意到我的自定义Web用户控件有一个事件调用OnError但是当我调整控件失败时它永远不会触发。该控件基本上是一个自定义gridview控件。我通过网络搜索网络用户控制事件处理,但我没有找到一篇解决我所寻找的文章。有人可以快速解释或指出我正确的方向吗?
thank
3 个解决方案
#1
2
I think i find my answers. http://msdn.microsoft.com/en-us/library/ms972975.aspx#usercontrols_topic5
我想我找到了答案。 http://msdn.microsoft.com/en-us/library/ms972975.aspx#usercontrols_topic5
It took me a while but i think this article is what i'm looking for. I hope it can help someone else as well.
我花了一段时间,但我认为这篇文章是我正在寻找的。我希望它也可以帮助别人。
#2
1
You didn't mention what flavour of ASP.NET, so I'll make the assumption of VB - C# is largely the same with the exception of how the event handler is attached.
你没有提到什么样的ASP.NET,所以我会假设VB - C#大致相同,除了事件处理程序的附加方式。
The normal pattern you would expect to see is something along these lines:
您期望看到的正常模式是这样的:
User Control "MyUserControl" CodeBehind
用户控制“MyUserControl”CodeBehind
Public Event MyEvent(ByVal Sender As Object, ByVal e As EventArgs)
Private Sub SomeMethodThatRaisesMyEvent()
RaiseEvent MyEvent(Me, New EventArgs)
End Sub
Page Designer Code
页面设计师代码
Private WithEvents MyUserControl1 As System.Web.UI.UserControls.MyUserControl
Page or other Control that wraps MyUserControl instance CodeBehind
包含MyUserControl实例CodeBehind的页面或其他Control
Private Sub MyUserControlEventHandler(ByVal Sender As Object, ByVal e As EventArgs) _
Handles MyUserControl.MyEvent
Response.Write("My event handled")
End Sub
In some instances, you see something called Event Bubbling which doesn't follow this kind of pattern exactly. But in the basic sense of handling events from a user control to a wrapper control or the page it sits in, that's how you would expect it to work.
在某些情况下,您会看到一些名为Event Bubbling的内容,它不会完全遵循这种模式。但是从处理从用户控件到包装器控件或它所在页面的事件的基本意义上,这就是你期望它工作的方式。
#3
0
I had an issue with a custom control that was throwing exceptions which were not firing Error
event. Thus I could not catch exceptions from this control and display appropriate message in the ASP.NET page.
我遇到了一个自定义控件的问题,该控件抛出了未触发错误事件的异常。因此,我无法从此控件中捕获异常并在ASP.NET页面中显示相应的消息。
Here is what I did. I wrapped the code in the custom control in a try..catch
block and fired the Error
event myself, like this:
这就是我做的。我将代码包装在try..catch块中的自定义控件中,并自行触发Error事件,如下所示:
// within the custom control try { // do something that raises an exception } catch (Exception ex) { OnError(EventArgs.Empty); // let parent ASP.NET page handle it in the // Error event }
The ASP.NET page was handling the exception using the Error
event like this:
ASP.NET页面使用Error事件处理异常,如下所示:
<script runat="server">
void MyCustomControl_Error(object source, EventArgs e)
{
MyCustomControl c = source as MyCustomControl;
if (c != null)
{
// Notice that you cannot retrieve the Exception
// using Server.GetLastError() as it will return null
Server.ClearError();
c.Visible = false;
// All I wanted to do in this case was to hide the control
}
}
</script>
<sd:MyCustomControl OnError="MyCustomControl_Error" runat="server" />
#1
2
I think i find my answers. http://msdn.microsoft.com/en-us/library/ms972975.aspx#usercontrols_topic5
我想我找到了答案。 http://msdn.microsoft.com/en-us/library/ms972975.aspx#usercontrols_topic5
It took me a while but i think this article is what i'm looking for. I hope it can help someone else as well.
我花了一段时间,但我认为这篇文章是我正在寻找的。我希望它也可以帮助别人。
#2
1
You didn't mention what flavour of ASP.NET, so I'll make the assumption of VB - C# is largely the same with the exception of how the event handler is attached.
你没有提到什么样的ASP.NET,所以我会假设VB - C#大致相同,除了事件处理程序的附加方式。
The normal pattern you would expect to see is something along these lines:
您期望看到的正常模式是这样的:
User Control "MyUserControl" CodeBehind
用户控制“MyUserControl”CodeBehind
Public Event MyEvent(ByVal Sender As Object, ByVal e As EventArgs)
Private Sub SomeMethodThatRaisesMyEvent()
RaiseEvent MyEvent(Me, New EventArgs)
End Sub
Page Designer Code
页面设计师代码
Private WithEvents MyUserControl1 As System.Web.UI.UserControls.MyUserControl
Page or other Control that wraps MyUserControl instance CodeBehind
包含MyUserControl实例CodeBehind的页面或其他Control
Private Sub MyUserControlEventHandler(ByVal Sender As Object, ByVal e As EventArgs) _
Handles MyUserControl.MyEvent
Response.Write("My event handled")
End Sub
In some instances, you see something called Event Bubbling which doesn't follow this kind of pattern exactly. But in the basic sense of handling events from a user control to a wrapper control or the page it sits in, that's how you would expect it to work.
在某些情况下,您会看到一些名为Event Bubbling的内容,它不会完全遵循这种模式。但是从处理从用户控件到包装器控件或它所在页面的事件的基本意义上,这就是你期望它工作的方式。
#3
0
I had an issue with a custom control that was throwing exceptions which were not firing Error
event. Thus I could not catch exceptions from this control and display appropriate message in the ASP.NET page.
我遇到了一个自定义控件的问题,该控件抛出了未触发错误事件的异常。因此,我无法从此控件中捕获异常并在ASP.NET页面中显示相应的消息。
Here is what I did. I wrapped the code in the custom control in a try..catch
block and fired the Error
event myself, like this:
这就是我做的。我将代码包装在try..catch块中的自定义控件中,并自行触发Error事件,如下所示:
// within the custom control try { // do something that raises an exception } catch (Exception ex) { OnError(EventArgs.Empty); // let parent ASP.NET page handle it in the // Error event }
The ASP.NET page was handling the exception using the Error
event like this:
ASP.NET页面使用Error事件处理异常,如下所示:
<script runat="server">
void MyCustomControl_Error(object source, EventArgs e)
{
MyCustomControl c = source as MyCustomControl;
if (c != null)
{
// Notice that you cannot retrieve the Exception
// using Server.GetLastError() as it will return null
Server.ClearError();
c.Visible = false;
// All I wanted to do in this case was to hide the control
}
}
</script>
<sd:MyCustomControl OnError="MyCustomControl_Error" runat="server" />