简体   繁体   English

在asp.net中为WebControl定义自定义事件

[英]define Custom Event for WebControl in asp.net

I need to define 3 events in a Custom Control as OnChange , OnSave , and OnDelete . 我需要在自定义控件中将3个事件定义为OnChangeOnSaveOnDelete I have a GridView and work with its rows. 我有一个GridView并使用它的行。

Can you help me and show me this code? 你能帮助我并告诉我这段代码吗?

Good article which can help you to achieve your task: 可以帮助您完成任务的好文章:

Custom Controls in Visual C# .NET Visual C#.NET中的自定义控件 在此输入图像描述

Step 1: Create the event handler in your control as below. 第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();
}

Step 2 : Utilize the event in the page where you register your control. 第2步:在您注册控件的页面中使用该事件。 The following code is going to be part of your page where your control is registered. 以下代码将成为您的控件注册页面的一部分。 If you register it, it will be triggered by the submit button of the control. 如果您注册它,它将由控件的提交按钮触发。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM