Friday, September 20, 2024

Creating an Online RSS News Aggregator with ASP.NET Part 3

Consuming a Syndication Feed in an ASP.NET Web Page

In order to test the syndication engine we just created, let’s build an online news aggregator that allows for any number of syndication feeds. The aggregator user interface will be fairly straightforward, comprising of three frames, as shown in Figure 2. In the left frame, a list of the various syndication feeds will be listed. In the top right frame, the news items for the selected syndication feed will be displayed. Finally, in the bottom right frame, the title and description of the selected news item feed will be displayed, with a link to the news item. Note that this UI is pretty much the de facto standard UI for aggregators of all kinds, including news aggregators, email clients and newsgroup readers.


Figure 2. A Screenshot of the News Aggregator User Interface

The first step is to create an HTML Web page that sets up the framed user interface. Fortunately, Visual Studio .NET 2003 makes this process quite simple, just choose to Add a New Item to your Web application Solution, choosing the new item type to be the Frameset. (I named this new file NewsAggregator.htm in my project. I left this as an HTML file as opposed to making it an ASP.NET Web page because this page will contain just the HTML to setup the three frames in the Web page. Each individual frame will be displaying an actual ASP.NET Web page.) This will launch the Frameset Template wizard shown in Figure 3. Simply pick the Nested Hierarchy option and click OK.


Figure 3. The Frameset Wizard in Visual Studio .NET 2003

The Frameset Template Wizard will then create an HTML Web page with the frame source already added. Merely set the src attribute of the left frame to DisplayFeeds.aspx, the URL of the ASP.NET Web page that will display the list of syndication feeds. That’s it for the NewsAggreator.htm page.

Over the next three sections we’ll look at creating the three components of the online news aggregator: DisplayFeeds.aspx, which displays the list of syndication feeds; DisplayNewsItems.aspx, which displays the news items for a particular syndication feed; and DisplayItem.aspx, which displays the details for a particular news item for a particular syndication feed.

Displaying the List of Syndication Feeds

We now need to create the DisplayFeeds.aspx ASP.NET Web page. This Web page will display the list of feeds we are subscribed to. For this demonstration, I decided to place the list of feeds in a database table called Feeds, although you could store the feeds just as well in an XML file. The Feeds table contains four fields:

  • FeedID-auto-incremented, integer primary key field used to uniquely identify each feed,
  • Title-a varchar(50) which stores the name of the feed,
  • URL-a varchar(150) that stores the URL to the RSS syndication feed, and
  • UpdateInterval-an integer field that specifies how often the feed should be updated, in minutes.
  • The DisplayFeeds.aspx Web page uses a DataGrid to display the list of syndication feeds. The DataGrid has a single HyperLinkColumn column, which displays the value of the Title field and links to the page DisplayNewsItems.aspx, passing along the FeedID field value in the querystring. The DataGrid declaration, with some peripheral markup omitted for brevity, can be seen below:

    <asp:DataGrid id="dgFeeds" runat="server"
    &nbsp&nbsp&nbsp&nbsp AutoGenerateColumns="False" ...>
    &nbsp ...
    &nbsp <Columns>
    &nbsp&nbsp&nbsp <asp:HyperLinkColumn Target="rtop"
    &nbsp&nbsp&nbsp&nbsp DataNavigateUrlField="FeedID"
    &nbsp&nbsp&nbsp&nbsp DataNavigateUrlFormatString="DisplayNewsItems.aspx?FeedID={0}"
    &nbsp&nbsp&nbsp&nbsp DataTextField="Title" HeaderText="RSS Feeds">
    &nbsp&nbsp </asp:HyperLinkColumn>
    &nbsp </Columns>
    </asp:DataGrid>

    The key thing to observe is the HyperLinkColumn. Note that its Target property is set so that when the user clicks on a syndication feed URL, the DisplayNewsItems.aspx Web page is opened in the top right frame. Also, the DataNavigateUrlField, DataNavigateUrlFormatString, and DataTextField properties are set so that the hyperlink displays the title of the syndication feed and, when clicked, it takes the user to DisplayNewsItems.aspx, passing along the value of the FeedID field in the querystring. (The code-behind class for this page simply accesses the list of feeds from the Feeds table ordered alphabetically by the Title field table and then binds the results to the DataGrid. For space considerations, this code is not presented here in the text of the article.)

    *This article originally appeared on the ASP.NET Dev Center at MSDN

    Scott Mitchell, author of five ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies for the past five years. An active member in the ASP and ASP.NET community, Scott is passionate about ASP and ASP.NET and enjoys helping others learn more about these exciting technologies. For more on the DataGrid, DataList, and Repeater controls, check out Scott’s book ASP.NET Data Web Controls Kick Start (ISBN: 0672325012). Read his blog at : http://scottonwriting.net

    Related Articles

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Latest Articles