Article

Build an RSS DataList Control in ASP.NET

Page: 1 2

DataList Templates

We now have 3 elements exposed to our DataList: title, link, and description. The display of these elements is handled within the Itemtemplate section of the DataList declaration. This process is very simple.

<asp:DataList ... >  
 
  <ItemTemplate>  
     ... template definition here  
  </ItemTemplate>  
 
</asp:DataList>

Within the ItemTemplate section, we can add markup and styles. At any point at which we want to display one of our elements (title, link, or description), we add the following inline code to tell ASP.NET to insert the contents of the element:

<%# DataBinder.Eval(Container.DataItem, "foo") %>

(Here, "foo" is changed to match the element to add.)

For example, here's a completed item template, which shows the title of a post in a large font, and the description underneath in a new paragraph break:

<sitepoint:RSSDataList runat="server">  
 
 <ItemTemplate>  
   <p><font size="4">  
     <%# DataBinder.Eval(Container.DataItem, "title") %>  
   </font></p>  
   <p>  
     <%# DataBinder.Eval(Container.DataItem, "description") %>  
   </p>  
 </ItemTemplate>  
 
</sitepoint:RSSDataList>

To conclude, let's walk through the process of consuming a feed using this new control.

Adding the Control to the Visual Studio Toolbox

  1. Right click on the control toolbox and select "Add / Remove Items�"
  2. From the Customize Toolbox dialog (shown below), click Browse� and select the assembly that contains the RSSDataList.cs class that holds the control:
  3. 1324_image1

  4. Select the RSSDataList control from the list, and make sure the check box is ticked. Click OK to add the control to the toolbox.

Defining the Control in the .aspx page

  1. Either drag the RSSDataList control onto your form, or add the following declarations to your .aspx page:

    <%@ Register TagPrefix="sitepoint" Namespace="SitePoint" Assembly="RSSDataList" %>  
     
    <sitepoint:RSSDataList id="RSSDataList1" runat="server">  
    </sitepoint:RSSDataList>

  2. Add the ItemTemplate we saw above to the RSSDataList tag:

    <%@ Register TagPrefix="sitepoint" Namespace="SitePoint" Assembly="RSSDataList" %>  
     
    <sitepoint:RSSDataList id="RSSDataList1" runat="server">  
     <ItemTemplate>  
       <p><font size="4">  
         <%# DataBinder.Eval(Container.DataItem, "title") %>  
       </font></p>  
       <p>  
         <%# DataBinder.Eval(Container.DataItem, "description") %>  
       </p>  
     </ItemTemplate>  
    </sitepoint:RSSDataList>

  3. Using the property selector in Visual Studio, you can define the feed you whish to associate with the control through the Location variable:

    1324_image2

    Alternatively, you can add this variable to the tag definition earlier:

    <sitepoint:RSSDataList id="RSSDataList1" Location="http://www.sitepoint.com/recent.rdf" runat="server">  
     ...  
    </sitepoint:RSSDataList>

    Or, of course, through code:

    private void Page_Load(object sender, System.EventArgs e)  
    {  
     RSSDataList1.Location = "http://www.sitepoint.com/recent.rdf";  
    }

Binding the Feed to the Control

Finally, you need to call the method bindToRSS() once you have set the Location property:

private void Page_Load(object sender, System.EventArgs e)  
{  
 RSSDataList1.Location = "http://www.sitepoint.com/recent.rdf";  RSSDataList1.bindToRSS();  
}

Make sure you have your XSLT file rss.xsl stored in the root of your application directory. That's all there is to displaying RSS and RDF files on your site!

1324_image3

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: