///<sarmmary>
///
/// </sarmmary>
// 绑定行政区域下拉列表
private void BindAreaDDL()
{
Un_Calt.Items.Clear();
//初始化下拉列表第一个值
Un_Calt.Items.Insert(0, new ListItem("===请选择===", "0"));
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from C_table
";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
//构造一个SqlDataAdapter
DataSet myDs = new DataSet();
myDa.Fill(myDs, "C_table");
myConn.Close();
//开始绑定DropDownList
//指定DropDownList使用的数据源
if (myDs.Tables.Count > 0)
{
var query1 = myDs.Tables[0].AsEnumerable().Where(x => x["ParentID"].ToString() == "0");
foreach (var item in query1)
{
ListItem li = new ListItem(item["PName"].ToString(), item["Classid"].ToString());
Un_Calt.Items.Add(li);
int index = 1;
AddChildListItem(myDs, item["Classid"].ToString(), index);
}
}
}
// 递归添加子项
private void AddChildListItem(DataSet ds, string pid, int index)
{
int tmp = index;
// 保存层级
// 计算前面的空白
string space_str = "";
for (int i = 0; i < index; i++)
{
space_str += "...";
}
var query1 = ds.Tables[0].AsEnumerable().Where(x => x["parentid"].ToString() == pid);
foreach (var item in query1)
{
ListItem li = new ListItem(space_str + item["PName"].ToString(), item["Classid"].ToString());
Un_Calt.Items.Add(li);
index++;
AddChildListItem(ds, item["Classid"].ToString(), index);
index = tmp;
// 还原层级
}
}
public DataSet GetDataSet()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select PName,classID from C_table Where ParentID=0 order by POrder desc";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
//构造一个SqlDataAdapter
DataSet myDs = new DataSet();
myConn.Close();
myDs.Tables[0].Columns[""].ToString();
//开始绑定DropDownList
//指定DropDownList使用的数据源
return myDs;
}
protected void Un_Calt_SelectedIndexChanged(object sender, EventArgs e)
{
string a = this.Un_Calt.Text.ToString();
Response.Write(a);
Response.End();
}
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindAreaDDL();
}
}
快给哥分