【问题标题】:how to retrieve the value from a dynamically created textbox using its ID in asp.net?如何使用 asp.net 中的 ID 从动态创建的文本框中检索值?
【发布时间】:2014-02-12 06:03:58
【问题描述】:

这就是我在表格中的单元格中添加动态文本框的方式。

我想使用它的 id 从文本框中检索值并将其存储在一个字符串中

生成一个sql查询

    For i As Integer = 1 To nov

        Dim tb As New TextBox()
        Dim tb2 As New DropDownList
        Dim tb3 As New TextBox()

        Dim tr As New TableRow()
        Dim tc As New TableCell()
        Dim tc2 As New TableCell()
        Dim tc3 As New TableCell()
        tb.ID = "txtName" + i.ToString()

        tb3.ID = "vv" + i.ToString()

        tb2.ID = "dd" + i.ToString()

        tb2.Items.Add("Int")
        tb2.Items.Add("Varchar")
        tb2.Items.Add("String")
        tblMain.Rows.Add(tr)
        tr.Cells.Add(tc)
        tc.Controls.Add(tb)


        tc2.Controls.Add(tb2)

        tr.Cells.Add(tc2)
        tc3.Controls.Add(tb3)
        tr.Cells.Add(tc3)

    Next
    Button2.Visible = True

【问题讨论】:

    标签: asp.net vb.net visual-studio-2008


    【解决方案1】:

    Dim str As String=DirectCast(Page.FindControl("txtName"+i), TextBox).Text

    在您的循环之外无法访问它,您必须使用您的代码来管理它。

    【讨论】:

    • 我收到了一个运行时错误,例如此对象引用未设置为对象的实例。
    • 在哪一行?同一行?
    • 你用同样的方法访问它的值吗?
    • 是的,我使用标签来显示字符串只是为了测试目的,最终的字符串将被实现为 sql 命令
    • 很抱歉,由于我正忙于办公室工作,目前无法调试您的代码,如果可能的话,我会在下班后给您解决方案。非常抱歉.. 一个建议“尝试使用 DirectCast 代替 CType”
    【解决方案2】:

    你可以使用Page.FindControl方法:

    “在页面命名容器中搜索具有指定标识符的服务器控件。”

    例如

    Dim t As TextBox= CType(Page.FindControl("txtName1"), TextBox)
    

    【讨论】:

      【解决方案3】:
      Sub TextControl()
              Dim txt As New TextBox
              txt.Text = ""
              txt.Name = "txt" & Val(no)
              txt.Size = New Size(44, 22)
              txt.Text = m
              txt.Location = New Point(tx, ty)
      
              GroupBox2.Controls.Add(txt)
              ty = ty + incr
              no = Val(no) + 1
          End Sub
      
      Dim textBoxValue As String = CType(GroupBox2.Controls(txt.Name), TextBox).Text.Trim
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        • 1970-01-01
        相关资源
        最近更新 更多