Reference object with Loop

Discussion in 'ASP .Net' started by Steven K, Sep 21, 2004.

  1. Steven K

    Steven K Guest

    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

    --
    Thanks in advance,

    Steven
    Steven K, Sep 21, 2004
    #1
    1. Advertising

  2. Steven K wrote:
    > 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)...

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
    Craig Deelsnyder, Sep 21, 2004
    #2
    1. Advertising

  3. Steven K

    Steven K Guest

    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



    "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    news:%...
    > Steven K wrote:
    > > 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)...
    >
    > --
    > Craig Deelsnyder
    > Microsoft MVP - ASP/ASP.NET
    Steven K, Sep 21, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    > news:%...
    >> Steven K wrote:
    >> > 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)...
    >>
    >> --
    >> Craig Deelsnyder
    >> Microsoft MVP - ASP/ASP.NET

    >
    >
    Ray Costanzo [MVP], Sep 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ce
    Replies:
    1
    Views:
    2,982
  2. Replies:
    1
    Views:
    476
    Mark Rae [MVP]
    Sep 20, 2007
  3. Arv
    Replies:
    15
    Views:
    917
    James Kanze
    Mar 7, 2008
  4. Eric Mahurin
    Replies:
    4
    Views:
    245
    David A. Black
    May 6, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page