asp.net动态设置文本框的值

时间:2013-05-31 04:37:26

标签: asp.net vb.net

我有一个非常独特的布局页面,其中36个文本框使用表格布局,以便它反映展厅的布局以及可以保留的展览表格。通过循环遍历表单键,我可以无缝地将它们全部保存到数据库中:

Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Dim tbname As String = ""
    Dim vendorName As String = ""
    Dim formkey As String = ""
    Dim conn As New SqlConnection(ConnectionStrings.Item("WebDB").ConnectionString)
    Dim cmd As New SqlCommand("UPDATE FloorPlan SET VendorName = @VendorName WHERE tbName = @tbName", conn)
        conn.Open()

    For Each o As Object In Request.Form.Keys
        formkey = o.ToString.Replace("ctl00$ContentPlaceHolder1$", "")
        If Left(formkey, 2) = "tb" Then
            tbname = formkey
            vendorName = Request(o.ToString)
            cmd.Parameters.AddWithValue("@VendorName", vendorName)
            cmd.Parameters.AddWithValue("@tbName", tbname)

            cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
            tbname = ""
            vendorName = ""
        End If
    Next
    conn.Close()
    conn.Dispose()
End Sub

我无法弄清楚如何在页面加载时动态加载值,它会在页面上找到文本框ID并填写值。 我尝试过这样的事情:

For Each r In ds1.Tables("SE").Rows
    CType(FindControl(r("tbname")), TextBox).Text = r("vendorname")
Next

但我只是将“对象引用未设置为实例...”错误。如果没有硬编码tb1.text = "blah"tb2.text = "acme"等,我应采取什么方法来正确加载值?

1 个答案:

答案 0 :(得分:0)

也许它与嵌套控件或母版页有关?可以尝试Atwood的递归查找控件:stackoverflow.com/questions/1457567/c-findcontrol