private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings[ "conn "]);
SqlDataAdapter da=new SqlDataAdapter( "select * from TreeView ",conn);
DataSet ds=new DataSet();
da.Fill(ds);
bindDDL(ddl,ds,0, " ");
}
}
private void bindDDL(DropDownList DDL,DataSet ds,int ParentID,string Tab)
{
DataView dv=new DataView(ds.Tables[0]);
dv.RowFilter= "ParentMenu= "+ParentID.ToString();
int count=0;
foreach(DataRowView Row in dv)
{
count++;
if(Tab.IndexOf( "├ ") >-1)
{
Tab=Tab.Replace( "├ ", "│ ");
}
if(Tab.IndexOf( "└ ") >-1)
{
Tab=Tab.Replace( "└ ", " ");
}
if(dv.Count==count)
{
Tab =Tab+ "└ ";
}
else
{
Tab =Tab+ "├ ";
}
DDL.Items.Add(new ListItem(Tab+Row[ "MenuName "].ToString(),Row[ "MenuID "].ToString()));
bindDDL(ddl,ds,int.Parse(Row[ "MenuID "].ToString()),Tab);
Tab= Tab.Remove(Tab.Length-1,1);
}
}
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings[ "conn "]);
SqlDataAdapter da=new SqlDataAdapter( "select * from TreeView ",conn);
DataSet ds=new DataSet();
da.Fill(ds);
bindDDL(ddl,ds,0, " ");
}
}
private void bindDDL(DropDownList DDL,DataSet ds,int ParentID,string Tab)
{
DataView dv=new DataView(ds.Tables[0]);
dv.RowFilter= "ParentMenu= "+ParentID.ToString();
int count=0;
foreach(DataRowView Row in dv)
{
count++;
if(Tab.IndexOf( "├ ") >-1)
{
Tab=Tab.Replace( "├ ", "│ ");
}
if(Tab.IndexOf( "└ ") >-1)
{
Tab=Tab.Replace( "└ ", " ");
}
if(dv.Count==count)
{
Tab =Tab+ "└ ";
}
else
{
Tab =Tab+ "├ ";
}
DDL.Items.Add(new ListItem(Tab+Row[ "MenuName "].ToString(),Row[ "MenuID "].ToString()));
bindDDL(ddl,ds,int.Parse(Row[ "MenuID "].ToString()),Tab);
Tab= Tab.Remove(Tab.Length-1,1);
}
}