|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Query;
using System.Xml.XLinq;
using System.Xml;
using System.Net;
using System.IO;
using System.Net.Sockets;
public partial class Widgets_RSSWidget : System.Web.UI.UserControl, IWidget
{
private IWidgetHost _Host;
protected void Page_Load(object sender, EventArgs e)
{
this.ShowFeeds();
}
private XElement _State;
private XElement State
{
get
{
if( _State == null ) _State = XElement.Parse(this._Host.GetState());
return _State;
}
}
public string Url
{
get { return State.Element("url").Value; }
set { State.Element("url").Value = value; }
}
public int Count
{
get { return State.Element("count") == null ? 3 : int.Parse( State.Element("count").Value ); }
set
{
if( State.Element("count") == null )
State.Add( new XElement("count", value) );
else
State.Element("count").Value = value.ToString();
}
}
private void ShowFeeds()
{
string url = State.Element("url").Value;
int count = State.Element("count") == null ? 3 : int.Parse( State.Element("count").Value );
var feed = Cache[url] as XElement;
if( feed == null )
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 3000;
using( WebResponse response = request.GetResponse() )
{
XmlTextReader reader = new XmlTextReader( response.GetResponseStream() );
feed = XElement.Load(reader);
if( feed == null ) return;
Cache.Insert(url, feed, null, DateTime.MaxValue, TimeSpan.FromMinutes(15));
}
}
catch
{
return;
}
}
XNamespace ns = "http://www.w3.org/2005/Atom";
// see if RSS or Atom
if( feed.Element("channel" ) != null )
FeedList.DataSource = (from item in feed.Element("channel").Elements("item")
select new
{
title = item.Element("title").Value,
link = item.Element("link").Value
}).Take(this.Count);
else if( feed.Element(ns + "entry") != null )
FeedList.DataSource = (from item in feed.Elements(ns + "entry")
select new
{
title = item.Element(ns + "title").Value,
link = item.Element(ns + "link").Attribute("href").Value
}).Take(this.Count);
FeedList.DataBind();
}
void IWidget.Init(IWidgetHost host)
{
this._Host = host;
}
void IWidget.ShowSettings()
{
SettingsPanel.Visible = true;
FeedCountDropDownList.SelectedIndex = -1;
FeedCountDropDownList.Items.FindByText( this.Count.ToString() ).Selected = true;
}
void IWidget.HideSettings()
{
SettingsPanel.Visible = false;
this.Count = int.Parse( FeedCountDropDownList.SelectedValue );
this.SaveState();
this.ShowFeeds();
}
void IWidget.Minimized()
{
}
void IWidget.Maximized()
{
}
void IWidget.Closed()
{
}
protected void SaveSettings_Click( object sender, EventArgs e )
{
(this as IWidget).HideSettings();
}
private void SaveState()
{
var xml = this.State.Xml;
this._Host.SaveState(xml);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.