将事件从 ASP.NET 用户控件公开到容器 Web 表单页面
如何实际将来自 asp.net usercontrol[ascx file] 的抽象事件公开到容器 Web 表单页面。这是我的场景,
我创建了一个 Webforms 用户控件,一个 ascx 文件,并放置了一个带有验证器的数据绑定复选框列表来验证它(我知道这可以由 Webforms 本身完成,为什么你会问用户控件,但这是一个场景)
现在我想向名为
OnValidating
的容器页面公开一个事件,该事件将产生验证结果
事件签名如下:
public delegate void Validating(object source,EventArgs e);
public event Validating OnValidating;
public void InvokeOnValidating(EventArgs e)
{
Validating handler = OnValidating;
if (handler != null) handler(this, e);
}
根据 msdn 文档,页面框架处理事件订阅和取消订阅。所以我需要做的就是在验证失败时调用该事件。太棒了,我很高兴,但是,
当所有其他公共属性都显示该事件时,我无法
- < p>当我输入
usercontrolid.
时,为什么我的事件调用者[InvokeOnValidating]
、事件委托[Validating]
显示在智能感知列表中这事件[OnValidating]
。我只想公开事件。 我还可以允许页面订阅用户控件内创建的事件 TextboxChanged 吗?如果是这样,请给我代码。
注意:我希望看到更多的代码而不是冗长的解释
How does one actually expose a abstracted event from asp.net usercontrol[ascx file] to the container webforms page. This is my scenario,
I created a webforms usercontrol a ascx file and put a databound checkboxlist with a validator to validate it(I know this could be done webforms itself why a usercontrol u ask, but it's a scenario)
Now i wanted to expose a event to the container page called
OnValidating
which would produce the result of validation
signature of the event is below:
public delegate void Validating(object source,EventArgs e);
public event Validating OnValidating;
public void InvokeOnValidating(EventArgs e)
{
Validating handler = OnValidating;
if (handler != null) handler(this, e);
}
As per the msdn documentation, the page framework handles the event subscribing and unsubscribing. So all i need to do was invoke the event when validation fails. Great i was happy but,
I couldn't show up the event in the properties window when all other public propertes did
Why the hell is my
event invoker[InvokeOnValidating]
,event delegate[Validating]
shown in intellisense list when i typeusercontrolid.
along with theevent[OnValidating]
. I want only the event to be exposed.Also can i allow page to subscribe to event TextboxChanged created inside the usercontrol? If so get me the code.
Note: I would love to see more code than lengthy explanations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在混合概念,您不需要委托来注册事件,请尝试此代码,我将解释这些更改并尝试在下面回答您的问题
在使用控件的页面上,请注意 On< /strong>,框架为所有事件添加了这个:
无法添加评论,但基本上如果您需要自定义事件参数,请执行以下操作:
You're mixing concepts, you don't need a delegate to register events, try this code, and i'll explain the changes and attempt to answer your questions below
On the page with the control use, notice the On, the framework adds this for all events:
Couldn't add on comment, but basically if you need custom event args do as such: