ASP.NET - List<> 绑定 DropDownList

时间:2021-03-02 17:20:46

代码:

         //声明泛型
List<category> inof = new List<category>();//二级分类 //声明类使用的对象类
public class category
{
public category(string id, string name)
{
Id = id;
Name = name;
} private string id; public string Id
{
get { return id; }
set { id = value; }
} private string name; public string Name
{
get { return name; }
set { name = value; }
}
} //获取信息
public void selectdatabind()
{
SqlDataReader sdr = SQLHelper.ExcuteReader("select ID, CategoryName from Tb_APCategory where ParentID = 1000", CommandType.Text);
while (sdr.Read())
{
SqlDataReader sdr_2 = SQLHelper.ExcuteReader("select ID, CategoryName from Tb_APCategory where ParentID = " + sdr["ID"].ToString() + "", CommandType.Text);
while (sdr_2.Read())
{
inof.Add(new category(sdr_2["ID"].ToString(), sdr_2["CategoryName"].ToString()));
}
}
} //使用
this.DropDownList2.DataSource = inof;
this.DropDownList2.DataValueField = "ID";
this.DropDownList2.DataTextField = "Name";
this.DropDownList2.DataBind();