繁体   English   中英

在asp.net的另一个单击事件中,如何从动态创建的文本框获取值?

[英]How to get values from dynamically created textbox's in another click event in asp.net?

我在页面加载事件中向面板添加了动态文本框,但是我无法访问按钮单击事件中的文本框,并且它显示面板控件在按钮单击事件中计数为零(0)。 请给我解决方案来访问按钮单击事件中的文本框值。

感谢您的建议。

在Init函数中添加控件:

<div id="Parent" runat="server">
</div>
<asp:Button ID="btnTest" runat="server" Text="Get text" OnClick="btnTest_Click" />

protected void Page_Init(object sender, EventArgs e)
{
    TextBox textInput = new TextBox();
    textInput.ID = "text1";
    textInput.Text = "Test";
    Parent.Controls.Add(textInput);
}

protected void btnTest_Click(object sender, EventArgs e)
{
    Response.Write((Parent.FindControl("text1") as TextBox).Text);
}

在特定按钮的EventHandler内插入

    TextBox MyTextBox=new TextBox();
//Assigning the textbox ID name 
MyTextBox.ID = "name" +""+ ViewState["val"] + i;
MyTextBox.Width = 440;
MyTextBox.Height = 40;
MyTextBox.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(MyTextBox);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM