?
Solved

Display multiple rows on asp.net website

Posted on 2010-08-13
6
Medium Priority
?
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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