Click here to Skip to main content
15,888,600 members
Articles / Web Development / HTML

Build a Google IG like AJAX Start Page in 7 days using ASP.NET AJAX and .NET 3.0

Rate me:
Please Sign up or sign in to vote.
4.80/5 (325 votes)
10 Mar 2010CPOL38 min read 1.8M   7.8K   1.1K  
Build a Start Page similar to Google IG in 7 nights using ASP.NET AJAX, .NET 3.0, LINQ, DLinq, and XLinq.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions