relative names

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am trying set up a bunch of fields that are differentiated by a number.

For example, I have 3 names which would be:

<asp: textbox id="name1" runat=server>
<asp:textbox id="name2" runat=server>
<asp:textbox id="name3" runat=server>

I want to be able to do a loop, something like:

for i = 1 to 3
if ("name" & i).text = ""
do something
next

or

Pass it to a sub, something like:

Call DoSomething("next" & i)

Is there someway to do this in VB.Net?

Thanks,

Tom
 
tshad said:
I am trying set up a bunch of fields that are differentiated by a number.

For example, I have 3 names which would be:

<asp: textbox id="name1" runat=server>
<asp:textbox id="name2" runat=server>
<asp:textbox id="name3" runat=server>

I want to be able to do a loop, something like:

for i = 1 to 3
if ("name" & i).text = ""
do something
next

or

Pass it to a sub, something like:

Call DoSomething("next" & i)

Is there someway to do this in VB.Net?

Thanks,

Tom

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

HTH,
Mythran
 
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
 
Back
Top