【问题标题】:ASP.NET VB - How to access data from dynamically created TextBoxASP.NET VB - 如何从动态创建的 TextBox 访问数据
【发布时间】:2018-02-16 21:46:30
【问题描述】:

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

例如:在 Aspx 页面上

<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

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

代码有效,除了,我如何在按钮点击时获取数据? 我做了一些谷歌搜索,但无法找到答案。 我已经尝试过page.FindControl("ab" & i),但我仍然无法获取值。

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

【问题讨论】:

    标签: asp.net vb.net dynamic


    【解决方案1】:

    使用直接广播访问您的单元格。这是我编写的用于访问动态生成的文本框的示例代码。

    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
    

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多