Reference object with Loop

S

Steven K

Hello (asp.net 2),

I have 3 controls on my content page that I trying to assign values to.

lblCompetitor01Name
lblCompetitor02Name
lblCompetitor03Name


In my codebehind I am using the following:

Dim lblFindControl As New Label(), ctrLoop As Integer

For ctrLoop = 1 to 3
lblFindControl.ID = "lblCompetitor0" & ctrCompetitor & "Name"
lblFindControl.Text = spSearchResults("strCompanyName")
Response.Write(lblFindControl.Text & "<br>")
Next ctrLoop

I am setting the ID value to the control on my page, but the text value is
not being populated.

On my old web pages I could use the following:
CType(FindControl("lblCompetitor0" & ctrCompetitor & "PriceType"),
Label).Text = spSearchResults("strCompanyPriceType")

Any help with this would be appreciated
 
C

Craig Deelsnyder

Steven said:
Hello (asp.net 2),

I have 3 controls on my content page that I trying to assign values to.

lblCompetitor01Name
lblCompetitor02Name
lblCompetitor03Name


In my codebehind I am using the following:

Dim lblFindControl As New Label(), ctrLoop As Integer

For ctrLoop = 1 to 3
lblFindControl.ID = "lblCompetitor0" & ctrCompetitor & "Name"
lblFindControl.Text = spSearchResults("strCompanyName")
Response.Write(lblFindControl.Text & "<br>")
Next ctrLoop

I am setting the ID value to the control on my page, but the text value is
not being populated.

On my old web pages I could use the following:
CType(FindControl("lblCompetitor0" & ctrCompetitor & "PriceType"),
Label).Text = spSearchResults("strCompanyPriceType")

Any help with this would be appreciated

Why not just do?

Dim results as String = spSearchResults("strCompanyName")
lblCompetitor01Name.Text = results
lblCompetitor02Name.Text = results
lblCompetitor03Name.Text = results

I assume these controls are defined in the html (aspx)...
 
S

Steven K

Hi Craig,

I have roughly 4 items per group (01, 02, 03). So it seems that using a
loop would reduce the amount of code by a third. Anyway is this bad
practice and is it even possible to do this?

old way
For ctrLoop = 1 to 3
CType(FindControl("lblCompetitor0" & ctrCompetitor & "PriceType"),
Label).Text = spSearchResults("strCompanyPriceType")
...
...
...
Next ctrLoop
 
R

Ray Costanzo [MVP]

Notice that you're using ctrLoop as your loop variable, yet you are
concatenating a variable named ctrCompetitor.

For _____ctrLoop________ = 1 to 3
lblFindControl.ID = "lblCompetitor0" & _______ctrCompetitor_______ &
"Name"


Ray at home


Steven K said:
Hi Craig,

I have roughly 4 items per group (01, 02, 03). So it seems that using a
loop would reduce the amount of code by a third. Anyway is this bad
practice and is it even possible to do this?

old way
For ctrLoop = 1 to 3
CType(FindControl("lblCompetitor0" & ctrCompetitor & "PriceType"),
Label).Text = spSearchResults("strCompanyPriceType")
...
...
...
Next ctrLoop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top