如何确定何时在父级的子级中单击按钮-ASP.NET [英] How do you determine when a button is clicked in the child on the parent - ASP.NET

查看:65
本文介绍了如何确定何时在父级的子级中单击按钮-ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的子用户控件中,我具有一个带OnRowCommand事件处理程序的gridview,该事件处理程序在单击编辑"按钮时执行.我想在子控件中单击编辑"按钮时将父控件中ASP.NET占位符控件的可见性设置为true.

In my child user control I have a gridview with an OnRowCommand eventhandler that is executed when Edit button is click. I want to set the visibility of an ASP.NET placeholder control in the parent to true when the Edit button is clicked in child control.

完成这项任务的最佳方法是什么?

What would be the best way to accomplish this task?

更新:
经过对互联网的更多研究后,我在孩子控件中创建了一个公共事件Eventhandler,并在触发OnRowCommand事件时触发了该事件.在父控件的page_load事件中,我将子用户控件事件映射到了父控件中的私有事件.

Update:
After a little bit more research on the internets, I create a public event Eventhandler in my child control and rasied the event when the OnRowCommand event was fired. In my page_load event of my parent control, i mapped the child user control event to an private event in my parent control.

儿童控制源代码:

public event EventHandler MyEvent;

protected void MyGridView_OnRowCommand(object sender, GridViewCommandEventsArgs e)
{
    if(MyEvent != null)
        MyEvent(sender, e);
}

父母控制源代码:

protected void Page_Load(object sender, EventArgs e)
{
    MyChildControl.MyEvent += new EventHandler(this.MyLocalEvent);
}

private void MyLocalEvent(object sender, EventArgs e)
{
    MyPlaceHolder.Visible = true;
    MyUpdatePanel.Update();
}

推荐答案

除了冒泡事件之外,还有两种方法值得考虑:

There are two methods in addition to bubbling the event worth considering:

a.在您的子用户控件中创建一个新事件.在父控件中侦听此事件.在子控件中触发此事件.

a. Create a new event in your child user control. Listen to this event in the parent control. Fire this event in the child control.

b.侦听父控件中的gridview事件OnRowCommand.

b. Listen to the gridview event OnRowCommand in the parent control.

方法a是优越的,因为泄漏的抽象较少. B快速又脏,但是如果这是一次性使用的用户控件,则不会产生负面影响.

Approach a is superior because there is less of the abstraction leaking through. B is quick and dirty, but if this is a one-time-use user control, it will not have a negative impact.

创建事件非常容易,许多程序都包含事件模板,这意味着您输入的唯一内容就是事件的名称(例如Resharper).

Creating events is fairly easy, many programs include templates for events which mean they only thing you type is the name of the event (e.g. Resharper).

这篇关于如何确定何时在父级的子级中单击按钮-ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆