将事件从 ASP.NET 用户控件公开到容器 Web 表单页面

发布于 2024-12-03 13:13:51 字数 998 浏览 1 评论 0原文

如何实际将来自 asp.net usercontrol[ascx file] 的抽象事件公开到容器 Web 表单页面。这是我的场景,

  1. 我创建了一个 Webforms 用户控件,一个 ascx 文件,并放置了一个带有验证器的数据绑定复选框列表来验证它(我知道这可以由 Webforms 本身完成,为什么你会问用户控件,但这是一个场景)

  2. 现在我想向名为 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 文档,页面框架处理事件订阅和取消订阅。所以我需要做的就是在验证失败时调用该事件。太棒了,我很高兴,但是,

  1. 当所有其他公共属性都显示该事件时,我无法

  2. < p>当我输入 usercontrolid. 时,为什么我的事件调用者[InvokeOnValidating]事件委托[Validating]显示在智能感知列表中这事件[OnValidating]。我只想公开事件。

  3. 我还可以允许页面订阅用户控件内创建的事件 TextboxChanged 吗?如果是这样,请给我代码。

注意:我希望看到更多的代码而不是冗长的解释

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-12-10 13:13:51

您正在混合概念,您不需要委托来注册事件,请尝试此代码,我将解释这些更改并尝试在下面回答您的问题

    public event EventHandler Validating;
    private void OnValidating(EventArgs ea)
    {
        var e = Validating;
        if (e != null)
            e(this, ea);
    }

在使用控件的页面上,请注意 On< /strong>,框架为所有事件添加了这个:

<uc:MyUserControl OnValidating="myhandlermethodinpage" />
  1. VS 对此并不是 100% 准确,有时控件的属性不会显示! Intelissence 更可靠
  2. 因为它们没有设置为私有,所以只有事件应该是公开的。
  3. 不确定这是否可行,您需要捕获控件中文本框的事件并使其引发自己的新事件。

无法添加评论,但基本上如果您需要自定义事件参数,请执行以下操作:

    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; }
}

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文