如何确定何时单击父级子级中的按钮 - ASP.NET

发布于 2024-07-11 03:59:10 字数 803 浏览 7 评论 0原文

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

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

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

子控件源代码:

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

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?

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.

Child Control source code:

public event EventHandler MyEvent;

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

Parent Control source code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-07-18 03:59:10

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

: 在您的子用户控件中创建一个新事件。 在父控件中监听此事件。 在子控件中触发此事件。

b. 监听父控件中的gridview事件OnRowCommand。

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

创建事件相当容易,许多程序都包含事件模板,这意味着您只需输入事件名称(例如 Resharper)。

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

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. Listen to the gridview event OnRowCommand in the parent control.

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.

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).

吹梦到西洲 2024-07-18 03:59:10

我突然想到,要么在子用户控件中创建一个新事件,该事件将在 OnRowCommand 触发时触发,要么使用 OnBubbleEvent - 我不记得确切的名称,但它是类似的东西。

Off the top of my head I would either create a new event within the child user control that would fire off when OnRowCommand fires, or use OnBubbleEvent - I can't remember the exact name but it's something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文