从暴露用户控件asp.net事件到容器Web窗体页 [英] Exposing Events from asp.net usercontrols to container web forms page

查看:121
本文介绍了从暴露用户控件asp.net事件到容器Web窗体页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎样才能真正揭露从asp.net用户控件[ASCX文件]一个抽象的事件容器web表单页面。这是我的情景,


  1. 我创建了一个web表单用户控件一个ASCX文件,并把数据绑定的CheckBoxList一个验证器来验证它(我知道可以这样做web表单本身为什么一个用户控件u向,但它是一个情景)


  2. 现在我想揭露事件 OnValidating 这将产生验证的结果。

  3. 容器页面被称为

事件的签名是如下:

 公共委托无效验证(对象源,EventArgs的);公共事件验证OnValidating;公共无效InvokeOnValidating(EventArgs的发送)
        {
            验证处理程序= OnValidating;
            如果(!=处理空值)处理器(这一点,E);
        }

根据MSDN文档,网页框架处理事件的订阅和取消订阅。因此,所有我需要做的是在验证失败时调用事件。很好,我很高兴,但是,


  1. 我不能显示在属性窗口中的事件时,其他所有公共propertes确实


  2. 凭啥是我的事件调用[InvokeOnValidating] 事件委托[确认] 显示在智能感知列表当我键入 usercontrolid。一起事件[OnValidating] 。我想被只露出事件。


  3. 也可以我允许网页订阅事件的用户控件内TextboxChanged产生的?如果是的话让我的code。


请注意:我希望能看到更多的code比冗长的解释


解决方案

您正在混合概念,您不需要委托注册事件,试试这个code,我会解释的变化和尝试回答你的问题如下

 公共事件的EventHandler验证;
    私人无效OnValidating(EventArgs的EA)
    {
        变种E =验证;
        如果(E!= NULL)
            E(这一点,EA);
    }

在与对照使用的页面,注意,框架增加了本作的所有事件:

 < UC:OnValidating的MyUserControl =myhandlermethodinpage/>


  1. VS不准确这个100%,有时控件的属性显示不出来! Intelissence更可靠

  2. 因为他们没有被设置为私有,只有事件应该是公开的。

  3. 不知道,这将是可能的,你需要捕捉文本框的情况下,在控制并使其提高它自己的新的事件。

无法添加上发表评论,但基本上如果你需要自定义事件参数做到这样:

 公共事件的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,

  1. 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)

  2. 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,

  1. I couldn't show up the event in the properties window when all other public propertes did

  2. Why the hell is my event invoker[InvokeOnValidating], event delegate[Validating] shown in intellisense list when i type usercontrolid. along with the event[OnValidating]. I want only the event to be exposed.

  3. 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" />

  1. VS is not 100% accurate with this, sometimes properties of controls don't show up! Intelissence is more reliable
  2. Because they weren't set as private, only the event should be public.
  3. 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窗体页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆