-
/// <summary>
-
/// if Type is 99,will show all departments,
-
/// type must be given value;
-
/// </summary>
-
public partial class DepartmentDropDownList : System.Web.UI.UserControl
-
{
-
private string _value;
-
-
#region skin or style
-
public string Css
-
{
-
set
-
{
-
ddlDepartment.CssClass = value;
-
}
-
}
-
public Unit Width
-
{
-
set
-
{
-
ddlDepartment.Width = value;
-
}
-
}
-
#endregion
-
-
-
/// <summary>
-
/// if Type<0,will show all departments
-
/// </summary>
-
public string Type { get; set; }
-
-
-
public string Value
-
{
-
get
-
{
-
return ddlDepartment.SelectedValue;
-
}
-
set
-
{
-
this._value = value;
-
BindData();
-
for (int i = 0; i < ddlDepartment.Items.Count; i++)
-
{
-
if (ddlDepartment.Items[i].Value == this._value)
-
{
-
ddlDepartment.Items[i].Selected = true;
-
}
-
}
-
}
-
-
}
-
-
/// <summary>
-
/// if you give a ListItem which has the same value of ListItem.Value,
-
/// the item of ddlDeparments will be selected,
-
/// the Text property of ListItem is not required
-
/// </summary>
-
public ListItem SelectedItem
-
{
-
get
-
{
-
return ddlDepartment.SelectedItem;
-
}
-
set
-
{
-
-
for (int i = 0; i < ddlDepartment.Items.Count; i++)
-
{
-
if (ddlDepartment.Items[i].Value == value.Value)
-
{
-
ddlDepartment.Items[i].Selected = true;
-
}
-
}
-
}
-
}
-
-
//public DepartmentDropDownList()
-
//{
-
// BindData(99);
-
//}
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
//if this control used in Ajax page,will not changed the item selected
-
//so should
-
-
// BindData();
-
for (int i = 0; i < ddlDepartment.Items.Count; i++)
-
{
-
if (ddlDepartment.Items[i].Value == this.Value)
-
{
-
ddlDepartment.Items[i].Selected = true;
-
}
-
}
-
-
-
-
}
-
-
-
-
/// <summary>
-
/// type is department type .if type==99, get all
-
/// </summary>
-
/// <param name="type"></param>
-
public void BindData(int type)
-
{
-
-
List<Department> table = new List<Department>();
-
//check the department type.
-
if (type==99)
-
{
-
table = DepartmentManager.GetAll();
-
}
-
else
-
{
-
table=DepartmentManager.GetAll().Where(p => p.DepartmentType == type).ToList();
-
}
-
string flag = "|--";
-
//clear prevent the item duplicate
-
ddlDepartment.Items.Clear();
-
ListItem itemTop = new ListItem("一级部门", "0");
-
ddlDepartment.Items.Add(itemTop);
-
foreach (var q in table.Where(p=>p.UpDepartmentId==0))
-
{
-
ListItem item = new ListItem(q.DepartmentName, q.DepartmentId.ToString());
-
ddlDepartment.Items.Add(item);
-
AddSubItem(item, table, flag);
-
//var subTable = table.Where(p => p.UpDepartmentId == q.DepartmentId).ToList();
-
}
-
-
}
-
-
public void BindData()
-
{
-
BindData(int.Parse(this.Type));
-
}
-
-
-
//add sub department item to DropDownList
-
private void AddSubItem(ListItem item, List<Department> table, string flag)
-
{
-
int upId =int.Parse( item.Value.ToString());
-
var subTable = table.Where(p => p.UpDepartmentId == upId).ToList();
-
foreach (var q in subTable)
-
{
-
ListItem subItem = new ListItem(flag+q.DepartmentName, q.DepartmentId.ToString());
-
ddlDepartment.Items.Add(subItem);
-
-
AddSubItem(subItem, table, flag+flag);
-
}
-
}
-
-
}
要求先要指定 Type,否则要发生异常。如果想要制作其他类似的话,只要修改数据源就可以了。
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>