The first page of the background management and the realization of a drop down l

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
  1. /// <summary>

  2. /// if Type is 99,will show all departments,

  3. /// type must be given value;

  4. /// </summary>

  5. public partial class DepartmentDropDownList : System.Web.UI.UserControl

  6. {

  7.     private string _value;

  8.  

  9.     #region skin or style

  10.     public string Css

  11.     {

  12.         set

  13.         {

  14.             ddlDepartment.CssClass = value;

  15.         }

  16.     }

  17.     public Unit Width

  18.     {

  19.         set

  20.         {

  21.             ddlDepartment.Width = value;

  22.         }

  23.     }

  24.     #endregion

  25.  

  26.  

  27.     /// <summary>

  28.     /// if Type<0,will show all departments

  29.     /// </summary>

  30.     public string Type { get; set; }

  31.  

  32.  

  33.     public string Value

  34.     {

  35.         get

  36.         {

  37.             return ddlDepartment.SelectedValue;

  38.         }

  39.         set

  40.         {

  41.             this._value = value;

  42.             BindData();

  43.             for (int i = 0; i <ddlDepartment.Items.Count; i++)

  44.             {

  45.                 if (ddlDepartment.Items[i].Value == this._value)

  46.                 {

  47.                     ddlDepartment.Items[i].Selected = true;

  48.                 }

  49.             }

  50.         }

  51.  

  52.     }

  53.  

  54.     /// <summary>

  55.     /// if you give a ListItem which has the same value of ListItem.Value,

  56.     /// the item of ddlDeparments will be selected,

  57.     /// the Text property of ListItem is not required

  58.     /// </summary>

  59.     public ListItem SelectedItem

  60.     {

  61.         get

  62.         {

  63.             return ddlDepartment.SelectedItem;

  64.         }

  65.         set

  66.         {

  67.  

  68.             for (int i = 0; i <ddlDepartment.Items.Count; i++)

  69.             {

  70.                 if (ddlDepartment.Items[i].Value == value.Value)

  71.                 {

  72.                     ddlDepartment.Items[i].Selected = true;

  73.                 }

  74.             }

  75.         }

  76.     }

  77.  

  78.     //public DepartmentDropDownList()

  79.     //{

  80.     // BindData(99);

  81.     //}

  82.  

  83.     protected void Page_Load(object sender, EventArgs e)

  84.     {

  85.         //if this control used in Ajax page,will not changed the item selected

  86.         //so should

  87.  

  88.            // BindData();

  89.         for (int i = 0; i <ddlDepartment.Items.Count; i++)

  90.         {

  91.             if (ddlDepartment.Items[i].Value == this.Value)

  92.             {

  93.                 ddlDepartment.Items[i].Selected = true;

  94.             }

  95.         }

  96.  

  97.  

  98.  

  99.     }

  100.  

  101.  

  102.  

  103.     /// <summary>

  104.     /// type is department type .if type==99, get all

  105.     /// </summary>

  106.     /// <param name="type"></param>

  107.     public void BindData(int type)

  108.     {

  109.  

  110.         List<Department> table = new List<Department>();

  111.         //check the department type.

  112.         if (type==99)

  113.         {

  114.             table = DepartmentManager.GetAll();

  115.         }

  116.         else

  117.         {

  118.             table=DepartmentManager.GetAll().Where(p => p.DepartmentType == type).ToList();

  119.         }

  120.         string flag = "|--";

  121.         //clear prevent the item duplicate

  122.         ddlDepartment.Items.Clear();

  123.         ListItem itemTop = new ListItem("First Department", "0");

  124.         ddlDepartment.Items.Add(itemTop);

  125.         foreach (var q in table.Where(p=>p.UpDepartmentId==0))

  126.         {

  127.             ListItem item = new ListItem(q.DepartmentName, q.DepartmentId.ToString());

  128.             ddlDepartment.Items.Add(item);

  129.             AddSubItem(item, table, flag);

  130.             //var subTable = table.Where(p => p.UpDepartmentId == q.DepartmentId).ToList();

  131.         }

  132.  

  133.     }

  134.  

  135.     public void BindData()

  136.     {

  137.         BindData(int.Parse(this.Type));

  138.     }

  139.  

  140.  

  141.     //add sub department item to DropDownList

  142.     private void AddSubItem(ListItem item, List<Department> table, string flag)

  143.     {

  144.         int upId =int.Parse( item.Value.ToString());

  145.         var subTable = table.Where(p => p.UpDepartmentId == upId).ToList();

  146.         foreach (var q in subTable)

  147.         {

  148.             ListItem subItem = new ListItem(flag+q.DepartmentName, q.DepartmentId.ToString());

  149.             ddlDepartment.Items.Add(subItem);

  150.  

  151.             AddSubItem(subItem, table, flag+flag);

  152.         }

  153.     }

  154.  

  155. }

Asked first to specify the Type, or to abnormal. If you want to make other similar words, as long as you can modify the data source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Gary at November 15, 2013 - 6:48 PM