SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Southampton, United Kingdom
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For each loop output in a label

    Hello folks,

    I am trying to loop multiple comments in two labels. For some reason, only the latest comment is being displayed in the label while when I output it using repsonse.write; all the comments display. I am pretty new to .NET and can't figure out what I am doing wrong. Any help would be great. Thanks.

    Code:
    <asp:Label ID="lblID" runat="server"></asp:Label>
            <asp:Label ID="lblComment" runat="server"></asp:Label>
    Code:
     Dim dt As DataTable
                            Dim dr As DataRow
                            dt = ds.Tables(0)
    
                            For Each dr In dt.Rows
    
                                Response.Write(dr("commentID").ToString())
                                Response.Write(dr("placementId").ToString())
                                Response.Write(dr("comment").ToString())
    
                                lblID.Text = dr("placementId").ToString()
                                lblComment.Text = dr("comment").ToString()
    
                            Next dr

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quite simple actually, you are overwriting the text stored in the labels each time you loop through a comment as you are using 'lblID.Text =' and 'lblComment.Text'.

    You should look into use a Repeater <asp:repeater>. Then you can bind your DataTable to the repeater. Let me know if you need help getting the repeater to work, and I'll be glad to assist. There should be many examples on the internet

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Southampton, United Kingdom
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved it using this


    lblID.Text += dr("placementId").ToString()

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Niraj Pandey View Post
    Solved it using this


    lblID.Text += dr("placementId").ToString()
    That is definitely one way to solve it. There are numerous reasons why I wouldn't approach it that way, and would go the router of a repeater, but nonetheless, good thing that you found an answer. I do strongly recommend researching repeaters (they are fantastic for these types of problems and make your coding very minimal, you usually just had it your datasource, bind it and in your aspx page you told it how to layout the information using an <itemtemplate>.

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Southampton, United Kingdom
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cpradio,

    Thank you for the reply and suggestion. I will definitely have a look when I can but at the moment I need to get things done and I am an absolute newbie in the whole .NET development and the site I am making has not security implications or even performance issues as it will be used by maybe 100-200 people at most. The code will still be looked by senior developers (who suggested to do that for now). But I will surely try to find how to do it using repeater. Thank you for your time.

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,651
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    If those senior devs suggested that or greenlight it to go to production they probably should not be senior devs.

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Southampton, United Kingdom
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha probably. Or probably they can't give much damn about a project they seem unwilling to take on. Otherwise they probably won't have left it for an absolute newbie in .NET environment to go throug a full life cycle development for it ;p. It's really an awkward situation for me. But I did found out how to use repeater for it now.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    If those senior devs suggested that or greenlight it to go to production they probably should not be senior devs.
    Thank you for being braver than I. I thought this the moment I read Niraj Pandey's response, but wasn't brave enough to say it. I personally would not greenlight the use the += over a repeater in this situation.

    Quote Originally Posted by Niraj Pandey View Post
    But I did found out how to use repeater for it now.
    Glad to hear it


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •