locked
Retrieve titles and dates from database query in list format RRS feed

  • Question

  • User-881997857 posted

    Hi everyone

    I am trying to implement a very simple news bulletin section to a web page. I have a database with a table called "news". It contains a few fields: id, title, timestamp, body, homepage. That's pretty much it. I also have a query called "homepage" which pulls out all of the records from the table which have "yes" under "homepage". That's the data I'm interested in - the records which I want to display on the homepage.

    I want to display them in this sort of format:

    • 19 Nov 2015 - We are launching our new hamburger today
    • 13 Nov 2015 - Get your plague vaccination now
    • 12 Nov 2015 - Today is World Kitten Day

    Just a simple bulleted list like that.

    So far, this is my code, and it's bringing up just one result, even though I have multiple results in the query. I'm not sure how to display an array of results. (Can you tell I'm not a .NET developer?!) What is the best type of control to use to get the results above, in a list format instead of in a table?

          <div class="row">
            <div class="col-md-12">
              <div class="panel panel-primary">
                <div class="panel-heading">
                  <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="D:\databases\news\news.mdb" SelectCommand="SELECT * FROM [homepage] ORDER BY timestamp DESC"></asp:AccessDataSource>
                  <h3 class="panel-title">Latest News</h3>
                </div>
                <div class="panel-body">
                  <asp:DetailsView ID="dvwNews" runat="server" AutoGenerateRows="false" DataKeyNames="ID" DataSourceID="AccessDataSource1" EnableModelValidation="True" CellPadding="1" GridLines="None">
              <Fields>
                <asp:TemplateField HeaderText="Timestamp" SortExpression="timestamp" ShowHeader="false" ControlStyle-BorderWidth="0" ControlStyle-BorderStyle="None">
                  <ItemTemplate>
                    <strong><asp:Label ID="Label2" runat="server" Text='<%# Bind("timestamp") %>'></asp:Label>:</strong> 
                  </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Title" SortExpression="title" ShowHeader="false" ControlStyle-BorderWidth="0">
                  <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label></p>
                  </ItemTemplate>
                </asp:TemplateField>
              </Fields>
            </asp:DetailsView>
                </div>
              </div>
            </div>
          </div>

    Thursday, November 19, 2015 6:17 AM

Answers

  • User-881997857 posted

    Just to confirm I have figured this out for myself (doesn't happen often!) by doing the following...

                <div class="panel-body">
                  <asp:AccessDataSource ID="newsDB" runat="server" DataFile="D:\databases\news\news.mdb" SelectCommand="SELECT * FROM [homepage] ORDER BY body ASC"></asp:AccessDataSource>
          <asp:ListView runat="server" ID="ListView1" 
        DataSourceID="newsDB">
      <LayoutTemplate>
        <ul>
          <li runat="server" id="itemPlaceholder"></li>
        </ul>
      </LayoutTemplate>
      <ItemTemplate>
        <%-- Data-bound content. --%>
        <li><strong><asp:Label ID="lblTimestamp" runat="server" Text='<%# Eval("timestamp", "{0:dd/MM/yyyy}")%>' /> &mdash;</strong> <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("title") %>' /></li>
      </ItemTemplate>
    </asp:ListView>
                </div>
              </div>
            </div>
          </div>

    Just posting my resolution in case anyone else has the same query...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2015 10:59 AM