伙计我在页面加载时动态创建文本框,但问题是我无法在按钮单击时从文本框中获取值。现在我的问题是我如何才能从动态创建的控件thx中获得价值。任何帮助,将不胜感激。我的Html是这样的:
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
我的代码就像这样
Partial Class TicketRound
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
Dim tb As New TextBox
tb.ID = "txt12"
tb.Text = "Child "
tb.Attributes.Add("runat", "Server")
form1.Controls.Add(tb)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim txtSurName As TextBox
txtSurName = form1.FindControl("txt12")
Response.Write(txtSurName.Text)
End Sub
End Class
请为我提出任何解决方案。
答案 0 :(得分:1)
您必须(重新)在每个回发上创建动态创建的控件,因此请替换:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
Dim tb As New TextBox
tb.ID = "txt12"
tb.Text = "Child "
tb.Attributes.Add("runat", "Server")' <-- redundant '
form1.Controls.Add(tb)
End If
End Sub
与
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tb As New TextBox
tb.ID = "txt12"
If Not IsPostBack Then tb.Text = "Child "
form1.Controls.Add(tb)
End Sub
有没有理由动态创建此控件?