Mythran said:
How about:
Response.Write( _
"Count of TextBoxes with prefix of 'name': " & _
GetDynamicTextBoxCount("name") _
)
' Note: Only works if the numbers are sequential. This is for
' learning only, really, so error checking and stuff is
' not displayed. Also, off top of my head so there might
' be an error or 2 in the code itself...
Function GetDynamicTextBoxCount(ByVal Prefix As String) As Integer
Dim i As Integer = 0
Dim txt As TextBox = DirectCast( _
Page.FindControl(Prefix & i), _
TextBox _
)
While Not txt Is Nothing
i += 1
txt = DirectCast(Page.FindControl(Prefix & i), TextBox)
End While
' Return the number of controls found.
Return IIf(i > 0, i - 1, i)
End Function
Showed me what I needed.
What I was trying to do was write out a number of records based on whether
the fields had data in them. There were 5 rows of 4 pieces of information
(name,address,businessname and phone). But is isn't set up as a table, in
this case.
So what I did was:
Dim txt As TextBox
for itemp = 1 to 5
txt = DirectCast(Page.FindControl("ReferenceName" & itemp),TextBox)
if txt is nothing or txt.text = "" then exit for
objCmd.Parameters.Clear()
with objCmd.Parameters
.Add("@ClientID",SqlDbType.VarChar,20).value = session("ClientID")
.Add("@ApplicantProfileID",SqlDbType.VarChar,20).value =
ApplicantProfileID
.Add("@Name",SqlDbType.VarChar,20).value = txt.Text
txt = DirectCast(Page.FindControl("ReferenceAddress" & itemp),TextBox)
.Add("@Address",SqlDbType.VarChar,20).value = txt.Text
txt = DirectCast(Page.FindControl("ReferenceBusinessName" &
itemp),TextBox)
.Add("@BusinessName",SqlDbType.VarChar,20).value = txt.Text
txt = DirectCast(Page.FindControl("ReferencePhone" & itemp),TextBox)
.Add("@Phone",SqlDbType.VarChar,20).value = txt.Text
end with
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
next
Works like a champ.
When I saw the FindControl and DirectCast, I realized how to do it. Similar
to how I process my Datagrids.
Thanks,
Tom