C# ASP.NET UserControl 和 DefaultEvent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET UserControl's and DefaultEvent
提问by Rob Cooper
Outline
大纲
OK, I have Google'd this and already expecting a big fat NO!!But I thought I should ask since I know sometimes there can be the odd little gem of knowledge lurking around in peoples heads ^_^
好的,我已经谷歌了这个,已经期待一个大胖子了!!但我想我应该问一下,因为我知道有时人们的头脑中可能潜伏着奇怪的小知识 ^_^
I am working my way through some excercises in a book for study, and this particular exercise is User Controls. I have cobbled together a control and would like to set the DefaultEvent for it (having done this for previous controls) so when I double-click it, the default event created is whatever I specify it to be.
我正在学习一本书中的一些练习,这个特殊的练习是用户控制。我拼凑了一个控件,并希望为其设置 DefaultEvent(已为以前的控件完成此操作),因此当我双击它时,创建的默认事件是我指定的任何事件。
NOTE:This is a standard User Control (.ascx), NOTa custom rendered control.
注意:这是标准的用户控件 (.ascx),而不是自定义呈现的控件。
Current Code
当前代码
Here is the class & event definition:
这是类和事件定义:
[System.ComponentModel.DefaultEvent("OKClicked")]
public partial class AddressBox : System.Web.UI.UserControl
{
public event EventHandler OKClicked;
Current Result
当前结果
Now, when I double click the the control when it is on a ASPX page, the following is created:
现在,当我在 ASPX 页面上双击该控件时,会创建以下内容:
protected void AddressBox1_Load(object sender, EventArgs e)
{
}
Not quite what I was expecting! So, my question:
和我期待的不太一样!所以,我的问题:
Is it possible to define a DefaultEvent for a UserControl? Is it a hack? If it's [not] supported, is there a reason?
是否可以为 UserControl 定义 DefaultEvent?这是一个黑客吗?如果[不]支持,有什么理由吗?
Side Note: How do we put underscores in code? I cant seem to put and escape char in?
旁注:我们如何在代码中放置下划线?我似乎无法放入和转义字符?
采纳答案by Darren Kopp
Here is a possibleanswer, without testing (like martin did).
这是一个可能的答案,无需测试(就像马丁所做的那样)。
In reflector, you will see that the DefaultEventAttribute allows itself to be inherited. In reflector, you see that the UserControl class has it's default event set to the Load event.
在反射器中,您将看到 DefaultEventAttribute 允许自身被继承。在反射器中,您会看到 UserControl 类将其默认事件设置为 Load 事件。
So the possible reason is that even though you are decorating your user control with the default event of OKClick, VS might still be thinking that the default event is load, as it's being inherited from UserControl whose default event is Load.
所以可能的原因是,即使你用 OKClick 的默认事件装饰你的用户控件,VS 可能仍然认为默认事件是加载,因为它是从默认事件是加载的 UserControl 继承的。
Just a high level guess at what might be happening.
只是对可能发生的事情的高级猜测。
回答by Rob Cooper
OK, I checked this out, Inheriting from WebControl rather than UserControl.. All worked fine.
好的,我检查了这个,从 WebControl 继承而不是 UserControl .. 一切正常。
Looks like Darren Kopptakes the crown for this one! Thanks for the input!
看起来达伦·科普为这个夺冠了!感谢您的输入!