簡體   English   中英

在asp.net中為WebControl定義自定義事件

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

我需要在自定義控件中將3個事件定義為OnChangeOnSaveOnDelete 我有一個GridView並使用它的行。

你能幫助我並告訴我這段代碼嗎?

可以幫助您完成任務的好文章:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM