Solved

Display multiple rows on asp.net website

Posted on 2010-08-13
6
402 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
call function 8 40
Compare site how does it work 4 51
Paging GridView 7 34
Problem to page 4 23
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

19 Experts available now in Live!

Get 1:1 Help Now