ASP.NET 3.5 ListView and DataPager : Making RSS Reader

ASP.NET 3.5 comes with bundle of new data bound controls. Among them ListView and DataPager are the ones I am going talk here. To me ListView is the enhanced from of DataRepeater. It has got everything you might need to create an app with your HTML/CSS skill set. Today you often visit blogs and you subscribe to RSS feed for your favourite ones. Here we are going to create the sample on “How to create RSS Reader”.

Ø Create a simple ASP.NET Project/Website.

Ø Add “XmlDataSource” to your page.

Ø Add the below configuration to your “XmlDataSource” for this blog,

<asp:XmlDataSource

    ID="xmlWrijuBlog"

    DataFile="https://blogs.msdn.com/wriju/rss.xml"

    XPath="rss/channel/item"

    runat="server">

</asp:XmlDataSource>

Note that the XPath is unique for any RSS feed and that is the standard.

Ø Now you need to add the ListView Control with below configuration

<asp:ListView

    ID="listWrijuBlog"

    DataSourceID="xmlWrijuBlog"

    ItemPlaceholderID="itemPlaceholder"

   runat="server">

This “ItemPlaceholderID” is the key here. You must need to have “asp:PlaceHolder” control under “LayoutTemplate” to be able to bind the data properly.

Ø Now let us have a look how the “LayoutTemplate” looks like

<LayoutTemplate>

    <br /><br /><h1> Wriju's Recent Blog</h1><br /><br />

    <asp:PlaceHolder ID="itemPlaceholder"

        runat="server"></asp:PlaceHolder>

</LayoutTemplate>

Here we have exact same ID for PlaceHolder

Ø Now how the individual records would look like, it is by ItemTemplate

<ItemTemplate>

    <h2><asp:HyperLink ID="hpLink"

            runat="server" Target="_blank"

            NavigateUrl='<%# XPath("link") %>'

        ><%# XPath("title") %></asp:HyperLink>

    </h2>

    <p><em>Published <%# XPath("pubDate") %></em></p>

    <p><%# XPath("description") %></p>

</ItemTemplate>

Ø Last one is optional if you need i.e., “ItemSeparatorTemplate”

<ItemSeparatorTemplate>

    <hr />

</ItemSeparatorTemplate>

Ø And now if you need the paging just go ahead and add DataPager control to it.

<asp:DataPager ID="DataPager1"

    PagedControlID="listWrijuBlog"

    PageSize="2"

    runat="server">

   

    <Fields>

        <asp:NextPreviousPagerField ButtonType="Button"

            ShowFirstPageButton="true" ShowLastPageButton="true"

            ShowNextPageButton="true" ShowPreviousPageButton="true" />

         <asp:NumericPagerField />

    </Fields>

</asp:DataPager>

Only thing you need to have here for a DataPager to say, “this is your paged control” though the attribute/property “listWrijuBlog”. This is nothing but your ListView control’s ID.

The source file is attached here.

For more visit https://asp.net

Namoskar!!!

Default.aspx