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

mpo*_*ani 8 c# asp.net custom-controls web-controls custom-event

我需要在自定义的控制,以确定3个事件OnChange,OnSaveOnDelete.我有一个GridView并使用它的行.

你能帮助我并告诉我这段代码吗?

Pra*_*ana 11

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

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();
}
Run Code Online (Sandbox Code Playgroud)

第2步:在您注册控件的页面中使用该事件.以下代码将成为您的控件注册页面的一部分.如果您注册它,它将由控件的提交按钮触发.

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