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.Reflection; using System.Collections.Specialized; public partial class Admin_Discounts : WebBlocks.AdministrativePage { public StringCollection SortOrdering { get { if (Session["SortOrdering"] == null) Session["SortOrdering"] = new StringCollection(); return (StringCollection)Session["SortOrdering"]; } } private string LastQuery { get { return (string)ViewState["LastQuery"]; } set { ViewState["LastQuery"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SortOrdering.Clear(); Search(""); } } private void Search() { if (LastQuery != null) Search(LastQuery); else Search(""); } private void Search(string query) { LastQuery = query; string srchQuery = "%"; if (txtSearch.Text.Length > 0) srchQuery += txtSearch.Text + "%"; DataTable dt = Database.Discounts_Search(srchQuery); DataView dv = dt.DefaultView; string Sort = ""; if (SortOrdering.Count > 0) { for (int i = SortOrdering.Count - 1; i >= 0; i--) Sort += SortOrdering[i] + ","; Sort = Sort.TrimEnd(','); } dv.Sort = Sort; dgDiscounts.DataSource = dv; dgDiscounts.DataBind(); } protected void btnSearch_Click(object sender, EventArgs e) { Search(txtSearch.Text); } protected void dgDiscounts_SortCommand(object source, DataGridSortCommandEventArgs e) { try { if (SortOrdering.Count == 0) { SortOrdering.Add(e.SortExpression); } else { int index = SortOrdering.IndexOf(e.SortExpression); if (index == SortOrdering.Count - 1) // first criteria { SortOrdering.Remove(e.SortExpression); SortOrdering.Add(e.SortExpression + " DESC"); } else if (index == -1) // doesnt exist yet, make this the first criteria { // remove its opposite int oppIndex = SortOrdering.IndexOf(e.SortExpression + " DESC"); if (oppIndex > -1) SortOrdering.RemoveAt(oppIndex); SortOrdering.Add(e.SortExpression); } else { // was clicked before, so now we remove it SortOrdering.Remove(e.SortExpression); // then make it the first criteria SortOrdering.Add(e.SortExpression); } } Search(); } catch (Exception ex) { string msg = ""; foreach (string s in SortOrdering) { msg += s + "::"; } throw new Exception(ex.Message + "||" + msg); } } protected void btnSearch_Click1(object sender, EventArgs e) { Search(txtSearch.Text); } }