niraj_pandey — 2012-07-29T12:29:19-04:00 — #1
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.
<asp:Label ID="lblID" runat="server"></asp:Label>
<asp:Label ID="lblComment" runat="server"></asp:Label>
Dim dt As DataTable
Dim dr As DataRow
dt = ds.Tables(0)
For Each dr In dt.Rows
lblID.Text = dr("placementId").ToString()
lblComment.Text = dr("comment").ToString()
cpradio — 2012-07-31T14:27:50-04:00 — #2
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
niraj_pandey — 2012-07-31T20:58:19-04:00 — #3
Solved it using this
lblID.Text += dr("placementId").ToString()
cpradio — 2012-08-01T05:59:52-04:00 — #4
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>.
niraj_pandey — 2012-08-01T06:30:59-04:00 — #5
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.
wwb_99 — 2012-08-01T15:43:36-04:00 — #6
If those senior devs suggested that or greenlight it to go to production they probably should not be senior devs.
niraj_pandey — 2012-08-01T16:37:22-04:00 — #7
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.
cpradio — 2012-08-01T16:47:11-04:00 — #8
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.
Glad to hear it
mittineague — 2014-09-17T22:40:16-04:00 — #9
This topic is now archived. It is frozen and cannot be changed in any way.