我也动态生成了一个按钮和它的事件。现在想要在Button的onClick事件中访问一个文本框(Generated Dynamically)。我可以这样做吗?
答案 0 :(得分:0)
您可以在类范围内声明它并在按钮事件handeler中使用它:
class MyClass
{
TextBox txtBox = null;
private void MyMethod()
{
var btn = new Button();
btn.Click += MyBtnEventHandler
...
txtBox = new TextBox();
}
protected void MyBtnEventHandler(object sender, .....)
{
if(txtBox != null) var data = txtBox.Text;
}
}
答案 1 :(得分:0)
如果您使用Page.Controls.Add()
或this.Controls.Add()
在页面中添加动态文本框,那么您是否尝试在按钮的onClick事件中使用Page.FindControl(textBoxId)
?您必须在运行时添加动态文本框的ID时设置它。