How do you determine when a button is clicked in the child on the parent - ASP.NET

狂风中的少年 提交于 2019-12-24 19:04:47

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/448363/how-do-you-determine-when-a-button-is-clicked-in-the-child-on-the-parent-asp-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!