public string username = string.Empty;
public int vip = 0;
private string _classcode;
public ArrayList array = new ArrayList();
public ProAndPro_Supply() { }
public ProAndPro_Supply(string iusername, int ivip)
{
username = iusername;
vip = ivip;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["droplistcount"] = 0;
Session["createcontrol"] = 0;
}
getClassInfo();// 动态生成dropDownList
}
/// <summary>
/// 动态生成dropDownList
/// </summary>
private void getClassInfo()
{
while (divClassInfo.Controls.Count > 0)
{
divClassInfo.Controls.RemoveAt(0);
}
if (Convert.ToInt32(ViewState["createcontrol"]) == 0)
{
DropDownList list = new DropDownList();
list.ID = "list0";
list.AutoPostBack = true;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
List<Infoclass> infos = InfoclassService.GetInfoclassFid(0);
list.DataSource = infos;
list.DataTextField = "CategoryName";
list.DataValueField = "IID";
list.DataBind();
ListItem item = new ListItem();
item.Text = "请选择";
item.Value = "0";
list.Items.Insert(0, item);
divClassInfo.Controls.Add(list);
array.Add(list);
ViewState["droplistcount"] = 1;
Session["createcontrol"] = array;
}
else
{
ArrayList controllist = new ArrayList();
controllist = (ArrayList)Session["createcontrol"];
//避免控件重复加载
for (int i = 0; i < controllist.Count; i++)
{
DropDownList downlist = (DropDownList)controllist[i];
divClassInfo.Controls.Add(downlist);
DropDownList list = (DropDownList)divClassInfo.Controls[i];
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
}
}
}
void list_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list1 = (DropDownList)sender;
ArrayList controllist = new ArrayList();
int index = divClassInfo.Controls.IndexOf(list1);
if (list1.SelectedValue == "0")
{
while (divClassInfo.Controls.Count > index + 1)
{
divClassInfo.Controls.RemoveAt(index + 1);
controllist = (ArrayList)Session["createcontrol"];
controllist.RemoveAt(index + 1);
Session["createcontrol"] = controllist;
}
return;
}
else
{
while (divClassInfo.Controls.Count > index + 2)
{
divClassInfo.Controls.RemoveAt(index + 2);
controllist = (ArrayList)Session["createcontrol"];
controllist.RemoveAt(index + 2);
Session["createcontrol"] = controllist;
}
}
controllist = (ArrayList)Session["createcontrol"];
ViewState["droplistcount"] = controllist.Count;
string key = list1.SelectedValue;
Infoclass infoClass = new Infoclass();
List<Infoclass> infoCls = InfoclassService.GetInfoclassFid(Convert.ToInt32(key));
if (infoCls.Count > 0)
{
//若该级已存在至需更新不用删除后添加,否则运行不正常
if (divClassInfo.Controls.IndexOf(list1) == divClassInfo.Controls.Count - 1)
{
DropDownList list = new DropDownList();
list.AutoPostBack = true;
list.DataSource = infoCls;
list.DataTextField = "CategoryName";
list.DataValueField = "IID";
list.DataBind();
ListItem item = new ListItem();
item.Text = "请选择";
item.Value = "0";
list.Items.Insert(0, item);
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
controllist = (ArrayList)Session["createcontrol"];
controllist.Add(list);
Session["createcontrol"] = controllist;
ViewState["droplistcount"] = controllist.Count;
divClassInfo.Controls.Add(list);
}
else
{
DropDownList list = (DropDownList)divClassInfo.Controls[divClassInfo.Controls.Count - 1];
list.AutoPostBack = true;
list.DataSource = infoCls;
list.DataTextField = "CategoryName";
list.DataValueField = "IID";
list.DataBind();
ListItem item = new ListItem();
item.Text = "请选择";
item.Value = "0";
list.Items.Insert(0, item);
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
}
}
else
{
if (divClassInfo.Controls.IndexOf(list1) != divClassInfo.Controls.Count - 1)
{
divClassInfo.Controls.RemoveAt(index + 1);
controllist = (ArrayList)Session["createcontrol"];
controllist.RemoveAt(index + 1);
Session["createcontrol"] = controllist;
}
}
}
public string classcode
{
get
{
int controlcount = divClassInfo.Controls.Count;
if (controlcount == 1)
{
DropDownList list = (DropDownList)divClassInfo.Controls[controlcount - 1];
_classcode = list.SelectedValue;
}
else
{
DropDownList list = (DropDownList)divClassInfo.Controls[divClassInfo.Controls.Count - 1];
if (list.SelectedValue == "0")
{
DropDownList list1 = (DropDownList)divClassInfo.Controls[divClassInfo.Controls.Count - 2];
_classcode = list1.SelectedValue;
}
else
{
_classcode = list.SelectedValue;
}
}
return _classcode;
}
}