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

时间:2013-05-07 05:10:52

标签: c# asp.net

我在页面加载事件中将动态文本框添加到我的面板,但是我无法在按钮单击事件中访问文本框,并且在按钮单击事件中显示面板控件计数ZERO(0)。请给我解决方案,以便在按钮点击事件中访问文本框值。

谢谢你的建议。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

特定按钮的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);