1

I have a page with a very distinct layout with 36 textboxes laid out using a table so that it reflects the layout of an exhibit hall and what exhibit tables can be reserved. I have been able save them all to the database seamlessly by looping through the form keys like this:

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

I can't figure out how to load the values on page load dynamically where it would locate the textbox id on the page and fill in the value. I have tried things like this:

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

but I just get the "object reference not set to an instance..." error. What approach should I take to get the values loaded properly without hard-coding tb1.text = "blah", tb2.text = "acme", etc...

Adrian
  • 2,825
  • 1
  • 22
  • 32
MarkOnDomo
  • 23
  • 3
  • 1
    The "object reference" error is most likely due to FindControl being unable to find the correct control. Does **r("tbname")** contain the asp.net id of the rendered control? Note that the Id (used by .Net to reference controls) and the Name (used to get values) are usually different. – Sam May 31 '13 at 04:48
  • I have labeled all my textboxes as tb1, tb2, tb3, etc.. and I have stored those IDs in the tbname column in my database. I was thinking I should get a match this way. I wondered if the controls aren't available to set yet on page load? – MarkOnDomo May 31 '13 at 04:57
  • 1
    They should be available on Page Load. I think the problem is that find control needs the rendered id (example: ctl00$ContentPlaceHolder1$tb1) – Sam May 31 '13 at 05:09
  • 1
    still using the ctype and findcontrol functions? I haven't ever seen that done before. – MarkOnDomo May 31 '13 at 05:20
  • Just looked it up and you are correct. Perhaps it has to do with nested controls or master pages? Could try Atwood's recursive find control: http://stackoverflow.com/questions/1457567/c-findcontrol – Sam May 31 '13 at 05:29
  • Great suggestion! That did it. Here's what I implemented for others that may run into the issue:'code' For Each r In ds1.Tables("SE").Rows Dim tb As TextBox = TryCast(FindControlRecursive(Page, r("tbname")), TextBox) tb.Text = r("vendorname") Next End Sub – MarkOnDomo May 31 '13 at 05:46
  • Added the recursive suggestion as an answer. – Sam May 31 '13 at 12:37

1 Answers1

0

Perhaps it has to do with nested controls or master pages? Could try Atwood's recursive find control: stackoverflow.com/questions/1457567/c-findcontrol

Community
  • 1
  • 1
Sam
  • 9,933
  • 12
  • 68
  • 104