using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyTunes.CachedDataset
{
    public partial class ArtistsCached : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ArtistCachedGridView.SortExpression))
                ArtistCachedGridView.Sort("artist", SortDirection.Ascending);

            if (!IsPostBack)
            {
                Global g = (Global)HttpContext.Current.ApplicationInstance;
                lblQuickLinks.Text = g.ArtistQuickLinks;
            }

            if (Request.Params["letter"] == null)
                ArtistCachedDataSource.FilterExpression = "";
            else
            {
                ArtistCachedGridView.PageIndex = 0;
                ArtistCachedDataSource.FilterExpression = "artist LIKE '" + Request.Params["letter"] + "%'";
            }
        }

        protected void ArtistCachedGridView_DataBound(object sender, EventArgs e)
        {
            lblCachedPageInfo.Text = string.Format("You are viewing page {0} of {1}...",
                ArtistCachedGridView.PageIndex + 1, ArtistCachedGridView.PageCount);

            lstPageSelectCached.Items.Clear();
            for (int i = 0; i < ArtistCachedGridView.PageCount; i++)
            {
                ListItem pageItem = new ListItem(string.Concat("Page ", i + 1), i.ToString());
                lstPageSelectCached.Items.Add(pageItem);
                if (i == ArtistCachedGridView.PageIndex)
                    pageItem.Selected = true;
            }
        }

        protected void lstPageSelectCached_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArtistCachedGridView.PageIndex = Convert.ToInt32(lstPageSelectCached.SelectedValue);
        }

        protected void btnArtistCachedFilterGo_Click(object sender, ImageClickEventArgs e)
        {
            UpdateFilter();
        }

        protected void txtArtistCachedFilter_TextChanged(object sender, EventArgs e)
        {
            UpdateFilter();
        }

        private void UpdateFilter()
        {
            if (!string.IsNullOrEmpty(txtArtistCachedFilter.Text))
                ArtistCachedDataSource.FilterExpression = "artist LIKE '%" + txtArtistCachedFilter.Text + "%'";
            else
                ArtistCachedDataSource.FilterExpression = null;

            ArtistCachedGridView.DataBind();
        }
    }
}