Solved

Display multiple rows on asp.net website

Posted on 2010-08-13
6
418 Views
Last Modified: 2012-05-10
I am trying to display multiple rows for my asp.net website.  I have the code behind that gets the data and I am passing that to a label tag.  It works but only shows the first record.  I have 3 records I want to display.  Here is my code behind:

   For I = 0 To dr.Tables("News").Rows.Count - 1
            'MsgBox(dr.Tables(0).Rows(I).Item(1).ToString())

            lblHeadline.Text = (dr.Tables(0).Rows(I).Item(1).ToString())
            lblDate.Text = (dr.Tables(0).Rows(I).Item(2).ToString())
   Next

Here is the inline code on the asp.net page:

  <asp:Label ID="lblHeadline" runat="server"></asp:Label>
    <br>
  <asp:Label ID="lblDate" runat="server"></asp:Label>

I am posting images of what it does now and what I want it to look like. What it is doing What it is doingPic2.png
0
Comment
Question by:thomashospital
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33431546
Why not use an repeater and add the datatable to the repeater as datasource and put the following code in the itemtemplate:

<%# Eval("NameOfYourColumn") %>
0
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 33431552
Have you tried to bind the table to an asp:repeater control?  Your inline code would be part of the ItemTemplate with databinding...
<asp:Label ID="lblHeadline" runat="server" Text='%# Eval("Headline") %>'></asp:Label>

The News & event could be in the HeaderTemplate..


0
 

Author Comment

by:thomashospital
ID: 33431891
Ok tried datarepeater but no data:

 Dim Repeater1 As New Repeater

 For I = 0 To dr.Tables("News").Rows.Count - 1
            '    'MsgBox(dr.Tables(0).Rows(I).Item(1).ToString())

            '    lblHeadline.Text = (dr.Tables(0).Rows(I).Item(1).ToString())
            '    lblDate.Text = (dr.Tables(0).Rows(I).Item(2).ToString())
           

  Next

Repeater1.DataSource = dr.Tables
Repeater1.DataBind()


and


 <asp:Label ID="lblDate" runat="server" Text='<%# Eval("Headline") %>'></asp:Label>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Stephan
ID: 33432084
you can do this on the front:

<asp:Repeater ID="repHeadlines" runat="server">
<HeaderTemplate>
 News &amp; Events
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Eval("Headline") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>

And this in the code-behind:

repHeadlines.DataSource = dr
repHeadlines.DataBind()
0
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 500 total points
ID: 33432099
This is an example of the repeater ...
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Example</title>

</head>

<body>

      <asp:Repeater ID="Repeater1" runat="server">

      <HeaderTemplate><h4>News & Press Center</h4><ul></HeaderTemplate>

      <SeparatorTemplate><p>&nbsp;</p></SeparatorTemplate>

       <ItemTemplate>

       <li>

       <asp:Label ID="lblHeadline" runat="server" Text='<%# Eval("Headline") %>'></asp:Label></li>

          <asp:Label ID="lblDate" runat="server" Text='<%# Eval("hlDate", "{0:D}") %>'></asp:Label>

       </ItemTemplate>

       <FooterTemplate></ul></FooterTemplate>

      </asp:Repeater>

    </div>

    </form>

</body>

</html>





.... Codebehind



Partial Class _Default

    Inherits System.Web.UI.Page





  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = False Then

      Dim dateHL As Date = Today.AddDays(-10)

      Dim dt As New Data.DataTable("Fred")

      dt.Columns.Add("Headline")

      dt.Columns.Add(New Data.DataColumn("hlDate", GetType(Date)))



      dt.Rows.Add("Apples", dateHL.AddDays(2))

      dt.Rows.Add("Oranges", dateHL.AddDays(7))

      dt.Rows.Add("Peaches", dateHL.AddDays(4))

      dt.Rows.Add("Tomatoes", dateHL)

      dt.AcceptChanges()

      Repeater1.DataSource = dt

      Repeater1.DataBind()

    End If

  End Sub





End Class





    <form id="form1" runat="server">

    <div>

Open in new window

0
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 33432121
???  the page did not paste correctly.  try this below ...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Required Test Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Repeater ID="Repeater1" runat="server">
      <HeaderTemplate><h4>News & Press Center</h4><ul></HeaderTemplate>
      <SeparatorTemplate><p>&nbsp;</p></SeparatorTemplate>
       <ItemTemplate>
       <li>
       <asp:Label ID="lblHeadline" runat="server" Text='<%# Eval("Headline") %>'></asp:Label></li>
          <asp:Label ID="lblDate" runat="server" Text='<%# Eval("hlDate", "{0:D}") %>'></asp:Label>
       </ItemTemplate>
       <FooterTemplate></ul></FooterTemplate>
      </asp:Repeater>
    </div>
    </form>
</body>
</html>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 47
SQL best practice question 3 36
Angular JS Route 3 52
ASP.net Javascript use multiple sliders 2 19
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now