I think this is probably the last of the fun user controls that I created for my web sites, but this one was one of the more important ones for my http://www.nocommonground.com website. When I created NoCommonGround, I wanted a site that would take all the info that I wanted, and put it into one place. I figured the easiest way to do that, was to integrate all the RSS feeds into one simple place. Thus, I needed an RSS feed user control.
There are a few bugs with this, I don't do complete checking for nulls every where, but I do in some of the more important places. I also import my own schema, because I was having problems pulling the schema down from other websites sometimes, and things weren't working right.
Lets have a look at the code:
<%@ Control Language="C#" ClassName="BlogAgg" %> <%@ Import Namespace="System.Data" %> <script runat="server"> public string BlogRSSFeed = ""; public string BlogTitle = ""; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (BlogRSSFeed == "") this.Visible = false; else { try { DataSet dsBlog = null; dsBlog = (DataSet)Cache.Get(BlogRSSFeed); if (dsBlog == null) { System.Net.WebProxy myProxy = new System.Net.WebProxy("http://MyProxy/", true); dsBlog = new DataSet(); System.Net.WebRequest wr = System.Net.WebRequest.Create(BlogRSSFeed); wr.Proxy = myProxy; System.Net.WebResponse wresp = wr.GetResponse(); dsBlog.ReadXmlSchema(Server.MapPath("~") + "\\App_Data\\rss_schema.xsd"); dsBlog.ReadXml(wresp.GetResponseStream(), XmlReadMode.IgnoreSchema); Cache.Add(BlogRSSFeed, dsBlog, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); } hlTitleLink.Text = BlogTitle; if (dsBlog.Tables["channel"].Rows[0]["link"] != System.DBNull.Value) hlTitleLink.NavigateUrl = (string)dsBlog.Tables["channel"].Rows[0]["link"]; hlTitleLink.Text = BlogTitle; hlTitle.Text = (string)dsBlog.Tables["item"].Rows[0]["title"]; hlTitle.NavigateUrl = (string)dsBlog.Tables["item"].Rows[0]["link"]; if (dsBlog.Tables["item"].Rows[0]["description"] != System.DBNull.Value) lblEntry.Text = (string)dsBlog.Tables["item"].Rows[0]["description"]; else lblEntry.Text = ""; int iRows = 10; if (dsBlog.Tables["item"].Rows.Count < iRows) iRows = dsBlog.Tables["item"].Rows.Count; LiteralControl lcStart = new LiteralControl("<ul>"); pnlRecent.Controls.Add(lcStart); for (int i = 1; i < iRows; i++) { LiteralControl lcOpen = new LiteralControl("<li>"); LiteralControl lcClose = new LiteralControl("</li>"); HyperLink hl = new HyperLink(); hl.Text = (string)dsBlog.Tables["item"].Rows[i]["Title"]; hl.NavigateUrl = (string)dsBlog.Tables["item"].Rows[i]["link"]; hl.Target = "_blank"; pnlRecent.Controls.Add(lcOpen); pnlRecent.Controls.Add(hl); pnlRecent.Controls.Add(lcClose); } LiteralControl lcStop = new LiteralControl("</ul>"); pnlRecent.Controls.Add(lcStop); } catch (Exception ex) { if ((Request.QueryString["debug"] != null) && (Request.QueryString["debug"].ToLower().Equals("true"))) { lblEntry.Text = ex.ToString(); } else { this.Visible = false; } } } } } </script>
And the HTML:
<h2 class="title"> <asp:HyperLink ID="hlTitleLink" runat="server"/></h2> <div id="BorderDiv"> <table> <tr> <td style="vertical-align:top; text-align:justify;" width="60%"> <h2> <asp:HyperLink ID="hlTitle" runat="server" Target="_blank" Text="No Recent Entries" /> </h2> <asp:Label ID="lblEntry" style="text-align: justify;" runat="server" > </asp:Label> </td> <td style="vertical-align:top;" width="40%"> <h2> Previous Entries:</h2> <asp:Panel ID="pnlRecent" runat="server" /> </td> </tr> </table> </div>
Overall, it's pretty simple, you can see an example at http://www.nocommonground.com The photo user controls at the top are just a slight variation on this code, and do some special processing on the data that is handed back so it'll show the picture correctly.
Enjoy!