在asp.net中定义自定义事件的WebControl [英] define Custom Event for WebControl in asp.net
问题描述
我需要定义一个自定义控件3事件的OnChange
,的OnSave
和 OnDelete
。
我有一个 GridView控件
和工作用其行。
I need to define 3 events in a Custom Control as OnChange
, OnSave
, and OnDelete
.
I have a GridView
and work with its rows.
您可以帮我,告诉我这个code?
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
第1步:中,如下所示控件创建事件处理程序
Step 1: Create the event handler in your control as below.
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();
}
第二步:利用在您注册控件的页事件。下面code将是你的页面,你的控件注册的一部分。如果你注册,它将被控制的提交按钮来触发。
Step 2 : Utilize the event in the page where you register your control. 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));
}
这篇关于在asp.net中定义自定义事件的WebControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!