gpt4 book ai didi

c# - 将 UserControl 中的事件添加到嵌套 UserControl 中的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 20:37:40 28 4
gpt4 key购买 nike

我有一个 UserControl,它包含另一个带有 Button 的 UserControl。我想在第一个 UserControl(父级)中向该按钮添加一个事件。我尝试这样做:

void Page_Init()
{
var btn = ChildControl.FindControl("SearchButton") as Button;
btn.Click += new EventHandler(this.SearchButton_Click);
}

btn 为空。我该怎么做?

最佳答案

FindControl不会递归搜索目标对象控件的子控件,因此首先获取嵌套控件,然后通过它的 ID 搜索按钮的子控件:

var btn = ChildControl.FindControl("NestedControl")
.FindControl("SearchButton") as Button;

btn.Click += new EventHandler(this.SearchButton_Click);

关于c# - 将 UserControl 中的事件添加到嵌套 UserControl 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4386929/

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