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
- Right click on the control toolbox and select "Add / Remove Items�"
- From the Customize Toolbox dialog (shown below), click Browse� and select the assembly that contains the
RSSDataList.csclass that holds the control: - Select the
RSSDataListcontrol 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
- Either drag the
RSSDataListcontrol onto your form, or add the following declarations to your.aspxpage:<%@ Register TagPrefix="sitepoint" Namespace="SitePoint" Assembly="RSSDataList" %>
<sitepoint:RSSDataList id="RSSDataList1" runat="server">
</sitepoint:RSSDataList> - Add the ItemTemplate we saw above to the
RSSDataListtag:<%@ 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> - Using the property selector in Visual Studio, you can define the feed you whish to associate with the control through the
Locationvariable:
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!
