gpt4 book ai didi

asp.net - 从子控件到父控件共享值(value)

转载 作者:行者123 更新时间:2023-12-02 21:32:51 25 4
gpt4 key购买 nike

我对此比较陌生,但这是我的问题。

在 asp.net 中,我有一个父控件和一个子控件。在子控件中,我有一个下拉列表。根据下拉列表的选定值,我想在父控件中切换面板的可见性。例如,如果我选择在子控件下拉列表中显示,我需要将 true 传递给父控件以使面板可见,反之亦然。我该怎么做呢。我读过这可以通过事件处理来完成,并且看到了某些场景,但我对此并不清楚。请帮忙!

谢谢。

最佳答案

引发父控件监听的事件。

在父控件的代码隐藏中,创建子控件类型的对象。像这样的东西:

private MyWebControl childControl;

然后在子控件中,定义一个事件

public event System.EventHandler SelectionChanged;

在 DropDownList 的 OnIndexChanged 事件中,完成处理后,引发事件:

if(this.SelectionChanged!= null)
{
this.SelectionChanged(this, new EventArgs());
// You can send the index of the DDL in the event args
}

在您的父控件中,连接事件。 Page_Init 很好

this.childControl.SelectionChanged+=new EventHandler(childControl_SelectionChanged);

仍在父控件中,定义您的方法

private void childControl_SelectionChanged(object sender, EventArgs e)
{
/// Do your processing here.
/// Grab the DDL's index from the EventArgs and do your processing

}

应该是您让它正常工作所需的一切!

关于asp.net - 从子控件到父控件共享值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3909269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com