Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBAdminPlugin/UserControls/OKBNamedItemList.ascx.cs @ 11291

Last change on this file since 11291 was 6084, checked in by gschwarz, 14 years ago

#1433 added UserControls

File size: 8.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7using WebControls.HLAdminService;
8using Wintellect.PowerCollections;
9using System.Collections;
10
11namespace WebControls {
12  // Class that implement the IComparer interface
13  // this is taken to sort the list ascending
14  class CompAsc : IComparer {
15    // Implement the IComparable interface.
16    public int Compare(object obj1, object obj2) {
17      ListItem a, b;
18      a = (ListItem)obj1;
19      b = (ListItem)obj2;
20     
21      return a.Text.CompareTo(b.Text);
22    }
23  }
24
25  // Class that implement the IComparer interface
26  // this is taken to sort the list descending
27  class CompDsc : IComparer {
28    public int Compare(object obj1, object obj2) {
29      ListItem a, b;
30      a = (ListItem)obj1;
31      b = (ListItem)obj2;
32      return b.Text.CompareTo(a.Text);
33    }
34  }
35
36  public partial class OKBNamedItemList : System.Web.UI.UserControl {
37
38    // delegate for the selectedIndexChanged
39    public delegate void OKBSelectedIndexChangedDelegate(Object sender, EventArgs e);
40
41    //event for the selected index changed
42    public event OKBSelectedIndexChangedDelegate OKBSelectedIndexChanged;
43
44    // global list of NAmedOKBItem the will show the items from this list
45    public static IList<NamedOKBItem> namedOKBItemList;
46    // enum to switch between the orderstate
47    public enum orderState { ascending, descending }
48    // orderstate variable
49    public static orderState ordered;
50    // List of the ItemList for the listcontrol
51    public static ArrayList ItemsList;
52
53    // selectedindex of the list
54    private  int selectedIndex;
55
56    // selectedvalue of the list
57    private string selectedValue;
58
59    /// <summary>
60    /// is always executed on page load
61    /// </summary>
62    /// <param name="sender"></param>
63    /// <param name="e"></param>
64    protected void Page_Load(object sender, EventArgs e) {
65      // if width or height not set, then set default width and height
66      if (UCWidth == null) {
67        UCWidth = "100%";
68      }
69      if (UCHeight == null) {
70        UCHeight = "100%";
71      }
72
73      // set the size of the whole control
74      this.Panel1.Width = new Unit(UCWidth);
75      this.Panel1.Height = new Unit(UCHeight);
76
77      // if the control is reload after any action
78      if (IsPostBack) {
79        // restore the data of the namedOKBITemList from viewstate
80        namedOKBItemList = (IList<NamedOKBItem>)ViewState["namedOKBItemList"];
81        // restore the data of the selected index from viewstate
82        this.selectedIndex = (int)ViewState["selectedIndex"];
83        // restore the data of the selected value from the viewstate
84        this.selectedValue = (string)ViewState["selectedValue"];
85        // restore the data of the selectedNamedOKBItem
86        this.SelectedNamedOKBItem = (NamedOKBItem)ViewState["selectedNamedOKBItem"];
87      } else {
88        // default orderstate
89        ordered = orderState.ascending;
90        // default selectedindex
91        this.selectedIndex = -1;
92        // default selected value
93        this.selectedValue = "";
94        // store the settings in the viewstate
95        ViewState["selectedIndex"] = this.selectedIndex;
96        ViewState["selectedValue"] = this.selectedValue;
97      }
98    }
99
100    /// <summary>
101    /// set the datalist for this control
102    /// it will take a List of NamedOKBItem
103    /// and the control do all other for you
104    /// </summary>
105    /// <param name="okbList">list of NamedOKBItem</param>
106    public void setNamedOKBItemList(IList<NamedOKBItem> okbList) {
107      namedOKBItemList = okbList;
108      // store it in the viewstate
109      ViewState["namedOKBItemList"] = namedOKBItemList;
110      // execute the fillItemList methode to fill the values and id's in the Listcontrol
111      fillItemList();
112    }
113
114    // get the List of NamedOKBItem
115    public IList<NamedOKBItem> getNamedOKBItemList() {
116      return namedOKBItemList;
117    }
118
119    /// <summary>
120    /// this methode take the List of NamedOKBItems and fill it in the Listcontrol
121    ///
122    /// </summary>
123    public void fillItemList() {
124      // Comparer for the list
125      IComparer comp = null;
126      // allocate new ArrayList
127      ItemsList = new ArrayList();
128      // clear old Items from List Control
129      lb_NamedOKBItems.Items.Clear();
130      // choose between the orderstate
131      if (ordered == orderState.ascending) {
132        comp = new CompAsc();
133      } else {
134        comp = new CompDsc();
135      }
136      // fill the list
137      if (namedOKBItemList != null) {
138        foreach (NamedOKBItem noi in namedOKBItemList) {
139          ListItem lItem = new ListItem(noi.Name, noi.Id.ToString());
140          ItemsList.Add(lItem);
141        }
142        // sort with the right comparer
143        ItemsList.Sort(comp);
144      }
145      foreach (ListItem li in ItemsList) {
146        lb_NamedOKBItems.Items.Add(li);
147      }
148      // at last bind the data
149      lb_NamedOKBItems.DataBind();
150    }
151
152    /// <summary>
153    /// this is the methode that will called when the ascending button is clicked
154    /// here the selected item will be stored in the global varaible
155    /// after the sort the selecteditem will again marked as select
156    /// </summary>
157    /// <param name="sender"></param>
158    /// <param name="e"></param>
159    protected void btn_Ascending_Click(object sender, EventArgs e) {
160      ordered = orderState.ascending;
161      ListItem selectedItem = null;
162      if (namedOKBItemList != null) {
163        if (lb_NamedOKBItems.SelectedIndex != -1) {
164          selectedItem = lb_NamedOKBItems.SelectedItem;
165         
166        }
167        fillItemList();
168        if (selectedItem != null) {
169          int index = lb_NamedOKBItems.Items.IndexOf(selectedItem);
170          lb_NamedOKBItems.SelectedIndex = index;
171        }
172       
173      }
174    }
175
176
177    /// <summary>
178    /// this is the methode that will called when the descending button is clicked
179    /// here the selected item will be stored in the global varaible
180    /// after the sort the selecteditem will again marked as select
181    /// </summary>
182    /// <param name="sender"></param>
183    /// <param name="e"></param>
184    protected void tn_Descending_Click(object sender, EventArgs e) {
185      ordered = orderState.descending;
186      ListItem selectedItem = null;
187      if (namedOKBItemList != null) {
188        if (lb_NamedOKBItems.SelectedIndex != -1) {
189          selectedItem = lb_NamedOKBItems.SelectedItem;
190        }
191        fillItemList();
192        if (selectedItem != null) {
193          int index = lb_NamedOKBItems.Items.IndexOf(selectedItem);
194          lb_NamedOKBItems.SelectedIndex = index;
195        }
196      }
197     
198    }
199
200    /// <summary>
201    /// this methode is called when the index is changed
202    /// </summary>
203    /// <param name="sender"></param>
204    /// <param name="e"></param>
205    protected void lb_NamedOKBItems_SelectedIndexChanged(object sender, EventArgs e) {
206      if (lb_NamedOKBItems.SelectedIndex == -1) {
207        this.SelectedNamedOKBItem = null;
208       
209      } else {
210        long id;
211        long.TryParse(lb_NamedOKBItems.SelectedValue, out id);
212        this.SelectedNamedOKBItem = namedOKBItemList.Where(x => x.Id == id).FirstOrDefault();
213 
214      }
215      ViewState["selectedNamedOKBItem"] = this.SelectedNamedOKBItem;
216      this.selectedIndex = lb_NamedOKBItems.SelectedIndex;
217      this.selectedValue = lb_NamedOKBItems.SelectedValue;
218      ViewState["selectedIndex"] = this.selectedIndex;
219      ViewState["selectedValue"] = this.selectedValue;
220      if (OKBSelectedIndexChanged != null) {
221        OKBSelectedIndexChanged(this, EventArgs.Empty);
222      }
223     
224    }
225
226    public NamedOKBItem SelectedNamedOKBItem { get; private set; }
227
228    // prpoerties for the controlsize
229    private string UCWidth;
230    private string UCHeight;
231
232    public string Width { get { return UCWidth; } set { UCWidth = value; } }
233    public string Height { get { return UCHeight; } set { UCHeight = value; } }
234
235  }
236}
Note: See TracBrowser for help on using the repository browser.