Click here to Skip to main content
15,882,113 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.Generic;
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.Data.DLinq;

public partial class FlickrWidget : System.Web.UI.UserControl, IWidget
{

    private const string FLICKR_API_KEY = "c705bfbf75e8d40f584c8a946cf0834c";
    private const string MOST_RECENT ="http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key="+FLICKR_API_KEY;
    private const string INTERESTING = "http://www.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key="+FLICKR_API_KEY;
    private const string ENTER_TAG ="http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key="+FLICKR_API_KEY+"&tags=";
    private const string FIND_BY_USERNAME = "http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key="+FLICKR_API_KEY+"&username=";
    private const string FIND_BY_EMAIL = "http://www.flickr.com/services/rest/?method=flickr.people.findByEmail&api_key="+FLICKR_API_KEY+"&find_email=";
    private const string PHOTOS_FROM_FLICKR_USER = "http://www.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key="+FLICKR_API_KEY+"&user_id=";

    private int Columns = 3;
    private int Rows = 3;
    
    private IWidgetHost _Host;

    private int PageIndex
    {
        get 
        { 
            object val = ViewState[this.ClientID + "_PageIndex"];
            if( null == val ) return 0;
            else return (int)val;
        }
        set { ViewState[this.ClientID + "_PageIndex"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {   
        if( this._Host.IsFirstLoad )
        {            
            this.LoadPictures();
            this.PageIndex = 0;
            this.ShowPictures(0);            
        }
        else
        {
            this.ShowPictures(this.PageIndex);            
        }
    }

    void IWidget.Init(IWidgetHost host)
    {
        this._Host = host;
    }

    void IWidget.ShowSettings()
    {
        settingsPanel.Visible = true;
    }
    void IWidget.HideSettings()
    {
        settingsPanel.Visible = false;
    }
    void IWidget.Minimized()
    {
    }
    void IWidget.Maximized()
    {
    }
    void IWidget.Closed()
    {
    }
    protected void mostRecentRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        this.SaveState();
        this.LoadPictures();
    }

    private void SaveState()
    {
    }

    private string LoadPictures()
    {
        string url = mostRecentRadioButton.Checked ? MOST_RECENT : INTERESTING;
        string cachedXml = Cache[url] as string;
        if( string.IsNullOrEmpty(cachedXml ) )
        {
            try
            {
                var xroot = XElement.Load(url);
                Cache.Insert(url, xroot.Xml, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));
                return xroot.Xml;
            }
            catch
            {
                return null;
            }
        }
        else
        {
            return cachedXml;
        }
    }

    private void ShowPictures(int pageIndex)
    {
        var xml = this.LoadPictures();
        if( string.IsNullOrEmpty(xml) ) return;
        var xroot = XElement.Parse(xml);
        var photos = (from photo in xroot.Element("photos").Elements("photo")
                    select new PhotoInfo
                    { 
                        Id = (string)photo.Attribute("id"),
                        Owner = (string)photo.Attribute("owner"),
                        Title = (string)photo.Attribute("title"),
                        Secret = (string)photo.Attribute("secret"),
                        Server = (string)photo.Attribute("server"),
                        Farm = (string)photo.Attribute("Farm"),
                        /*IsPublic = (bool)photo.Attribute("ispublic"),
                        IsFriend = (bool)photo.Attribute("isfriend"),
                        IsFamily = (bool)photo.Attribute("isfamily")*/
                    }).Skip(pageIndex*Columns*Rows).Take(Columns*Rows);
        
        HtmlTable table = new HtmlTable();
        table.Align = "center";
        var row = 0;
        var col = 0;
        var count = 0;
        foreach( var photo in photos )
        {
            if( col == 0 )
                table.Rows.Add( new HtmlTableRow() );

            var cell = new HtmlTableCell();
            
            var img = new HtmlImage();
            img.Src = photo.PhotoUrl(true);
            img.Width = img.Height = 75;
            img.Border = 0;

            var link = new HtmlGenericControl("a");
            link.Attributes["href"] = photo.PhotoPageUrl;      
            link.Attributes["Target"] = "_blank";
            link.Attributes["Title"] = photo.Title;
            link.Controls.Add(img);
            
            cell.Controls.Add(link);
            table.Rows[row].Cells.Add(cell);

            col ++;
            if( col == Columns )
            {
                col = 0; row ++;
            }

            count ++;
        }

        photoPanel.Controls.Clear();
        photoPanel.Controls.Add(table);

        if( pageIndex == 0 )
        {
            this.ShowPrevious.Visible = false;
            this.ShowNext.Visible = true;
        }
        else
        {
            this.ShowPrevious.Visible = true;
        }
        if( count < Columns*Rows )
        {
            this.ShowNext.Visible = false;
        }
    }
    protected void ShowPrevious_Click(object sender, EventArgs e)
    {
        this.PageIndex --;        
        this.ShowPictures(this.PageIndex);
    }
    protected void ShowNext_Click(object sender, EventArgs e)
    {
        this.PageIndex ++;
        this.ShowPictures(this.PageIndex);
    }
}

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