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.Collections.Specialized; public partial class Admin_SearchOrders : WebBlocks.AdministrativePage { public StringCollection SortOrdering { get { if (Session["SortOrdering"] == null) Session["SortOrdering"] = new StringCollection(); return (StringCollection)Session["SortOrdering"]; } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = Database.OrderStatus_GetAll(); ddlSearchStatus.DataSource = dt; ddlSearchStatus.DataBind(); ddlSearchStatus.Items.Insert(0, new ListItem("All Orders", "")); } } private void Search() { int? OrderID = (txtOrderID.Text.Length == 0 ? null : (int?)Int32.Parse(txtOrderID.Text)); int? OrderStatusID = (ddlSearchStatus.SelectedValue == "" ? null : (int?)Int32.Parse(ddlSearchStatus.SelectedValue)); string FirstName = (txtSearchFirstName.Text.Length == 0 ? "%" : "%" + txtSearchFirstName.Text + "%"); string LastName = (txtSearchLastName.Text.Length == 0 ? "%" : "%" + txtSearchLastName.Text + "%"); DateTime? OrderDateFrom = (txtOrderDateFrom.Text == "" ? null : (DateTime ?)DateTime.Parse(txtOrderDateFrom.Text)); DateTime? OrderDateTo = (txtOrderDateTo.Text == "" ? null : (DateTime?)DateTime.Parse(txtOrderDateTo.Text).AddDays(1)); DataTable dt = Database.Orders_Search(OrderID, OrderStatusID, FirstName, LastName, OrderDateFrom, OrderDateTo); DataView dv = dt.DefaultView; if (SortOrdering.Count > 0) { string Sort = ""; for (int i = SortOrdering.Count - 1; i >= 0; i--) Sort += SortOrdering[i] + ","; Sort = Sort.TrimEnd(','); dv.Sort = Sort; } dgOrders.DataSource = dv; dgOrders.DataBind(); } protected void dgOrders_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(); } }