ASP.NET VB - 如何从动态创建的TextBox访问数据

时间:2017-09-08 04:07:52

标签: asp.net vb.net dynamic

首先,我是ASP.NET(VB)的初学者程序员。如标题中所述,我如何访问动态创建的texbox的值

例如:On Aspx Page

<asp:Table id= "Table1" runat="server">
</asp:Table>

在Page_Load上的Aspx.Vb页面上,我有

dim i as integer = 0

While i < 3
  Dim tempCell as New TableCell
  Dim tempCell2 as New TableCell
  Dim temprow as New TableRow 

  tempCell.Controls.Add(New LiteralControl("<asp:TextBox id = 'aa" & i & "' runat="server">this is value for ab " & i & "</asp:TextBox>"))
  tempCell2.Controls.Add(New LiteralControl("<asp:TextBox id = 'ab" & i & "' runat="server">this is value for ab " & i & "</asp:TextBox>"))

  temprow.Cells.Add(tempCell) 
  temprow.Cells.Add(tempCell2)

  Table1.Rows.Add(temprow)

  i = i + 1
End While

所以,这大致是我想要做的。

代码有效,但是,如何获取按钮点击数据? 我做了一些谷歌搜索,但无法找到一个回答者。 我已经尝试过该页面.FindControl(&#34; ab&#34;&amp; i),但我仍然无法获得该值。

我在哪里做错了?提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用directcast访问您的单元格。这是我为访问动态生成的文本框而编写的示例代码。

Dim txt = New TextBox()
txt.Name = "name1"
txt.Size = New Size(200, 70)
txt.Location = New Point(40, 40)
txt.Text = "I am a new textbox"
Me.Controls.Add(txt)
DirectCast(Me.Controls("name1"), TextBox).Text = "Some stuff here" 'The magic happens here