[英]C#, User Control, Events - User Control's Control Event Overriding?
In my C# program, I have a Custom Control with a label and a button. 在我的C#程序中,我有一个带有标签和按钮的自定义控件。 How can I setup my control so that when a user clicks the button it overrides the Custom Control's Click event?
如何设置我的控件,以便当用户单击按钮时它会覆盖自定义控件的Click事件?
Simply call the control's OnClick() method: 只需调用控件的OnClick()方法:
private void button1_Click(object sender, EventArgs e) {
this.OnClick(e);
}
Which fires the control's Click event. 这会触发控件的Click事件。
You can write it like this: 你可以像这样写:
public new event EventHandler Click {
add { button1.Click += value; }
remove { button1.Click -= value; }
}
Here is full article which explain you how you can achieve it : Exposing Custom event from custom control 这篇完整的文章向您解释如何实现它: 从自定义控件中公开自定义事件
Following is step for the drodown used in user control and exposing event you need to do same for you button , you will get more idea after reading above link 以下是用于用户控制和曝光事件的步骤,您需要为您执行相同操作按钮,阅读上面的链接后您会得到更多的想法
Step 1 : Register event in user control cs file
步骤1:在用户控制cs文件中注册事件
public event EventHandler DrpChange;
Step 2 : Virtual function to handle raised event in usercontrol cs file
第2步:虚拟函数来处理usercontrol cs文件中的引发事件
public virtual void OnDropDownChange()
{
if (DrpChange != null)
{
this.DrpChange(this, EventArgs.Empty);
}
}
Step 3 : Register on Change Event of dropdown in ASCX.CS file
步骤3:在ASCX.CS文件中的下拉列表的更改事件中注册
protected void ddlDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
this.OnDropDownChange();
}
Step 4 : Use User control on page and utilize expose custom event .Aspx page
第4步:在页面上使用用户控件并使用公开自定义事件.Aspx页面
<uc1:commondropdowncontrol autopostback="true" drpchange="usrDrp_DrpChange" id="usrDrp" labletext="Country" runat="server">
</uc1:commondropdowncontrol></div>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.