【问题标题】:define Custom Event for WebControl in asp.net在 asp.net 中为 WebControl 定义自定义事件
【发布时间】:2012-05-22 06:13:38
【问题描述】:

我需要在自定义控件中定义 3 个事件为 OnChangeOnSaveOnDelete。 我有一个 GridView 并使用它的行。

你能帮我看看这段代码吗?

【问题讨论】:

    标签: c# asp.net custom-controls web-controls custom-event


    【解决方案1】:

    可以帮助您完成任务的好文章:

    Custom Controls in Visual C# .NET

    第 1 步:在您的控件中创建事件处理程序,如下所示。

    public event SubmitClickedHandler SubmitClicked;
    
    // Add a protected method called OnSubmitClicked().
    // You may use this in child classes instead of adding
    // event handlers.
    protected virtual void OnSubmitClicked()
    {
        // If an event has no subscribers registered, it will
        // evaluate to null. The test checks that the value is not
        // null, ensuring that there are subscribers before
        // calling the event itself.
        if (SubmitClicked != null)
        {
            SubmitClicked();  // Notify Subscribers
        }
    }
    
    // Handler for Submit Button. Do some validation before
    // calling the event.
    private void btnSubmit_Click(object sender, System.EventArgs e)
    {
        OnSubmitClicked();
    }
    

    第 2 步:利用您注册控件的页面中的事件。以下代码将成为您的控件注册页面的一部分。如果注册了,会被控件的提交按钮触发。

    // Handle the SubmitClicked Event
    private void SubmitClicked()
    {
        MessageBox.Show(String.Format("Hello, {0}!",
            submitButtonControl.UserName));
    }
    

    【讨论】:

    • 最纯粹的活动。正是我想要开始的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多