我有一个布局非常独特的页面,其中有 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"
等...