?
Solved

Display multiple rows on asp.net website

Posted on 2010-08-13
6
Medium Priority
?
465 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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