using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using WebControls.HLAdminService; using Wintellect.PowerCollections; using System.Collections; namespace WebControls { // Class that implement the IComparer interface // this is taken to sort the list ascending class CompAsc : IComparer { // Implement the IComparable interface. public int Compare(object obj1, object obj2) { ListItem a, b; a = (ListItem)obj1; b = (ListItem)obj2; return a.Text.CompareTo(b.Text); } } // Class that implement the IComparer interface // this is taken to sort the list descending class CompDsc : IComparer { public int Compare(object obj1, object obj2) { ListItem a, b; a = (ListItem)obj1; b = (ListItem)obj2; return b.Text.CompareTo(a.Text); } } public partial class OKBNamedItemList : System.Web.UI.UserControl { // delegate for the selectedIndexChanged public delegate void OKBSelectedIndexChangedDelegate(Object sender, EventArgs e); //event for the selected index changed public event OKBSelectedIndexChangedDelegate OKBSelectedIndexChanged; // global list of NAmedOKBItem the will show the items from this list public static IList namedOKBItemList; // enum to switch between the orderstate public enum orderState { ascending, descending } // orderstate variable public static orderState ordered; // List of the ItemList for the listcontrol public static ArrayList ItemsList; // selectedindex of the list private int selectedIndex; // selectedvalue of the list private string selectedValue; /// /// is always executed on page load /// /// /// protected void Page_Load(object sender, EventArgs e) { // if width or height not set, then set default width and height if (UCWidth == null) { UCWidth = "100%"; } if (UCHeight == null) { UCHeight = "100%"; } // set the size of the whole control this.Panel1.Width = new Unit(UCWidth); this.Panel1.Height = new Unit(UCHeight); // if the control is reload after any action if (IsPostBack) { // restore the data of the namedOKBITemList from viewstate namedOKBItemList = (IList)ViewState["namedOKBItemList"]; // restore the data of the selected index from viewstate this.selectedIndex = (int)ViewState["selectedIndex"]; // restore the data of the selected value from the viewstate this.selectedValue = (string)ViewState["selectedValue"]; // restore the data of the selectedNamedOKBItem this.SelectedNamedOKBItem = (NamedOKBItem)ViewState["selectedNamedOKBItem"]; } else { // default orderstate ordered = orderState.ascending; // default selectedindex this.selectedIndex = -1; // default selected value this.selectedValue = ""; // store the settings in the viewstate ViewState["selectedIndex"] = this.selectedIndex; ViewState["selectedValue"] = this.selectedValue; } } /// /// set the datalist for this control /// it will take a List of NamedOKBItem /// and the control do all other for you /// /// list of NamedOKBItem public void setNamedOKBItemList(IList okbList) { namedOKBItemList = okbList; // store it in the viewstate ViewState["namedOKBItemList"] = namedOKBItemList; // execute the fillItemList methode to fill the values and id's in the Listcontrol fillItemList(); } // get the List of NamedOKBItem public IList getNamedOKBItemList() { return namedOKBItemList; } /// /// this methode take the List of NamedOKBItems and fill it in the Listcontrol /// /// public void fillItemList() { // Comparer for the list IComparer comp = null; // allocate new ArrayList ItemsList = new ArrayList(); // clear old Items from List Control lb_NamedOKBItems.Items.Clear(); // choose between the orderstate if (ordered == orderState.ascending) { comp = new CompAsc(); } else { comp = new CompDsc(); } // fill the list if (namedOKBItemList != null) { foreach (NamedOKBItem noi in namedOKBItemList) { ListItem lItem = new ListItem(noi.Name, noi.Id.ToString()); ItemsList.Add(lItem); } // sort with the right comparer ItemsList.Sort(comp); } foreach (ListItem li in ItemsList) { lb_NamedOKBItems.Items.Add(li); } // at last bind the data lb_NamedOKBItems.DataBind(); } /// /// this is the methode that will called when the ascending button is clicked /// here the selected item will be stored in the global varaible /// after the sort the selecteditem will again marked as select /// /// /// protected void btn_Ascending_Click(object sender, EventArgs e) { ordered = orderState.ascending; ListItem selectedItem = null; if (namedOKBItemList != null) { if (lb_NamedOKBItems.SelectedIndex != -1) { selectedItem = lb_NamedOKBItems.SelectedItem; } fillItemList(); if (selectedItem != null) { int index = lb_NamedOKBItems.Items.IndexOf(selectedItem); lb_NamedOKBItems.SelectedIndex = index; } } } /// /// this is the methode that will called when the descending button is clicked /// here the selected item will be stored in the global varaible /// after the sort the selecteditem will again marked as select /// /// /// protected void tn_Descending_Click(object sender, EventArgs e) { ordered = orderState.descending; ListItem selectedItem = null; if (namedOKBItemList != null) { if (lb_NamedOKBItems.SelectedIndex != -1) { selectedItem = lb_NamedOKBItems.SelectedItem; } fillItemList(); if (selectedItem != null) { int index = lb_NamedOKBItems.Items.IndexOf(selectedItem); lb_NamedOKBItems.SelectedIndex = index; } } } /// /// this methode is called when the index is changed /// /// /// protected void lb_NamedOKBItems_SelectedIndexChanged(object sender, EventArgs e) { if (lb_NamedOKBItems.SelectedIndex == -1) { this.SelectedNamedOKBItem = null; } else { long id; long.TryParse(lb_NamedOKBItems.SelectedValue, out id); this.SelectedNamedOKBItem = namedOKBItemList.Where(x => x.Id == id).FirstOrDefault(); } ViewState["selectedNamedOKBItem"] = this.SelectedNamedOKBItem; this.selectedIndex = lb_NamedOKBItems.SelectedIndex; this.selectedValue = lb_NamedOKBItems.SelectedValue; ViewState["selectedIndex"] = this.selectedIndex; ViewState["selectedValue"] = this.selectedValue; if (OKBSelectedIndexChanged != null) { OKBSelectedIndexChanged(this, EventArgs.Empty); } } public NamedOKBItem SelectedNamedOKBItem { get; private set; } // prpoerties for the controlsize private string UCWidth; private string UCHeight; public string Width { get { return UCWidth; } set { UCWidth = value; } } public string Height { get { return UCHeight; } set { UCHeight = value; } } } }