[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Display multiple rows on asp.net website

Posted on 2010-08-13
6
Medium Priority
?
456 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
Industry Leaders: 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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