问题描述
怎样才能真正揭露从asp.net用户控件[ASCX文件]一个抽象的事件容器web表单页面。这是我的情景,
-
我创建了一个web表单用户控件一个ASCX文件,并把数据绑定的CheckBoxList一个验证器来验证它(我知道可以这样做web表单本身为什么一个用户控件u向,但它是一个情景)
-
现在我想揭露事件
OnValidating
这将产生验证的结果。 容器页面被称为
事件的签名是如下:
公共委托无效验证(对象源,EventArgs的);公共事件验证OnValidating;公共无效InvokeOnValidating(EventArgs的发送)
{
验证处理程序= OnValidating;
如果(!=处理空值)处理器(这一点,E);
}
根据MSDN文档,网页框架处理事件的订阅和取消订阅。因此,所有我需要做的是在验证失败时调用事件。很好,我很高兴,但是,
-
我不能显示在属性窗口中的事件时,其他所有公共propertes确实
-
凭啥是我的
事件调用[InvokeOnValidating]
,事件委托[确认]
显示在智能感知列表当我键入usercontrolid。
与一起事件[OnValidating]
。我想被只露出事件。 -
也可以我允许网页订阅事件的用户控件内TextboxChanged产生的?如果是的话让我的code。
请注意:我希望能看到更多的code比冗长的解释
您正在混合概念,您不需要委托注册事件,试试这个code,我会解释的变化和尝试回答你的问题如下
公共事件的EventHandler验证;
私人无效OnValidating(EventArgs的EA)
{
变种E =验证;
如果(E!= NULL)
E(这一点,EA);
}
在与对照使用的页面,注意在,框架增加了本作的所有事件:
< UC:OnValidating的MyUserControl =myhandlermethodinpage/>
- VS不准确这个100%,有时控件的属性显示不出来! Intelissence更可靠
- 因为他们没有被设置为私有,只有事件应该是公开的。
- 不知道,这将是可能的,你需要捕捉文本框的情况下,在控制并使其提高它自己的新的事件。
无法添加上发表评论,但基本上如果你需要自定义事件参数做到这样:
公共事件的EventHandler< CustomEventArgs>验证;
私人无效OnValidating(CustomEventArgs EA)
{
变种E =验证;
如果(E!= NULL)
E(这一点,EA);
}
公共类CustomEventArgs:EventArgs的{
公众诠释myProperty的{搞定;组; }
}
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
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
public event EventHandler Validating;
private void OnValidating(EventArgs ea)
{
var e = Validating;
if (e != null)
e(this, ea);
}
On the page with the control use, notice the On, the framework adds this for all events:
<uc:MyUserControl OnValidating="myhandlermethodinpage" />
- VS is not 100% accurate with this, sometimes properties of controls don't show up! Intelissence is more reliable
- Because they weren't set as private, only the event should be public.
- Not sure that will be possible, you'll need to capture the event of the textbox in the control and make it raise it's own new event.
Couldn't add on comment, but basically if you need custom event args do as such:
public event EventHandler<CustomEventArgs> Validating;
private void OnValidating(CustomEventArgs ea)
{
var e = Validating;
if (e != null)
e(this, ea);
}
public class CustomEventArgs:EventArgs{
public int MyProperty { get; set; }
}
这篇关于从暴露用户控件asp.net事件到容器Web窗体页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!