xmdDatasource

本文介绍如何利用 ASP.NET 中的 TreeView 控件构建一个多级导航菜单,并展示了如何通过后台代码填充 XML 数据源来动态生成菜单结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" ExpandDepth="2">
</asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" XPath="/*/*"></asp:XmlDataSource>


public partial class WebControls_Navigation : System.Web.UI.UserControl
{
    DataTable dtArea;
    DataTable dtCountry;
    DataTable dtCity;
    protected void Page_Load(object sender, EventArgs e)
    {   
        NavigationBLL navigationBll = new NavigationBLL();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><navigation></navigation>");
        XmlNode root = xmlDoc.SelectSingleNode("navigation");

        XmlElement xeBaseInfo = xmlDoc.CreateElement("基本资料区域");
        root.AppendChild(xeBaseInfo);

        dtArea = navigationBll.GetAreaFromView();
        dtCountry = navigationBll.GetCountryFromView();
        dtCity = navigationBll.GetCityFromView();

        dtArea = TreeURLSet(dtArea, "areaID", "AreaID");
        dtCountry = TreeURLSet(dtCountry, "countryID", "CountryID");
        dtCity = TreeURLSet(dtCity, "cityID", "CityID");

        if (dtArea.Rows.Count > 0)//地区
        {
            XmlElement[] xeArea = new XmlElement[dtArea.Rows.Count];            
            for (int i = 0; i < dtArea.Rows.Count; i++)
            {   
                xeArea[i] = xmlDoc.CreateElement(dtArea.Rows[i]["AreaName"].ToString());
                dtCountry.DefaultView.Sort = "AreaID";//地区到国家
                DataRowView[] dtCountryView = dtCountry.DefaultView.FindRows(new object[] { dtArea.Rows[i]["AreaID"].ToString() });
                if (dtCountryView.Length > 0)
                {
                    XmlElement[] xeCountry = new XmlElement[dtCountryView.Length];
                    for (int j = 0; j < dtCountryView.Length; j++)
                    {
                        xeCountry[j] = xmlDoc.CreateElement(dtCountryView[j]["ContryName"].ToString());
                        dtCity.DefaultView.Sort = "CountryID";//国家到城市
                        DataRowView[] dtCityView = dtCity.DefaultView.FindRows(new object[] { dtCountryView[j]["CountryID"].ToString() });
                        if (dtCityView.Length > 0)
                        {
                            XmlElement[] xeCity = new XmlElement[dtCityView.Length];
                            for (int m = 0; m < dtCityView.Length; m++)
                            {
                                xeCity[m] = xmlDoc.CreateElement(dtCityView[m]["CityName"].ToString());

                                XmlElement[] xeCityChild = new XmlElement[4];
                                xeCityChild[0] = xmlDoc.CreateElement("地接社");
                                //xeCityChild[0].InnerXml = "InnerXml";
                                //xeCityChild[0].InnerText = "InnerText";
                                //Response.Write(xeCityChild[0].Name);//didi
                                //Response.Write(xeCityChild[0].Value);//xx
                                xeCityChild[1] = xmlDoc.CreateElement("景点资料");
                                xeCityChild[2] = xmlDoc.CreateElement("酒店资料");
                                xeCityChild[3] = xmlDoc.CreateElement("客户资料");
                                xeCity[m].AppendChild(xeCityChild[0]);
                                xeCity[m].AppendChild(xeCityChild[1]);
                                xeCity[m].AppendChild(xeCityChild[2]);
                                xeCity[m].AppendChild(xeCityChild[3]);

                                xeCountry[j].AppendChild(xeCity[m]);
                            }
                        }
                        xeArea[i].AppendChild(xeCountry[j]);
                    }
                }
                xeBaseInfo.AppendChild(xeArea[i]);
            }            
        }

        XmlDataSource1.Data = xmlDoc.InnerXml;
        TreeView1.DataSourceID = "XmlDataSource1";
        TreeView1.DataBind();        

        //for (int i = 0; i < TreeView1.Nodes.Count; i++)//1个--基本资料区域
        //{
        //    TreeView1.Nodes[i].NavigateUrl = "~/BaseInfo/Area";
        //    for (int j = 0; j < TreeView1.Nodes[i].ChildNodes.Count; j++)//地区
        //    {
        //        TreeView1.Nodes[i].ChildNodes[j].NavigateUrl = "~/BaseInfo/" + dtArea.Rows[j]["treeURL"].ToString();
        //        for (int m = 0; m < TreeView1.Nodes[i].ChildNodes[j].ChildNodes.Count; m++)//国家
        //        {
        //            TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].NavigateUrl = "City" + dtCountry.Rows[m]["treeURL"].ToString();
        //            for (int n = 0; n < TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes.Count; n++)//城市
        //            {
        //                //TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].NavigateUrl = "CityChildHaveNoOperate" + dtCity.Rows[n]["treeURL"].ToString();

        //                TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].NavigateUrl = "CityChild--DiJieShe" + dtCity.Rows[n]["treeURL"].ToString();
        //                TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[1].NavigateUrl = "CityChild--JingDianZiLiao" + dtCity.Rows[n]["treeURL"].ToString();
        //                TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[2].NavigateUrl = "CityChild--JiuDianZiLiao" + dtCity.Rows[n]["treeURL"].ToString();
        //                TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[3].NavigateUrl = "CityChild--KeHuZiLiao" + dtCity.Rows[n]["treeURL"].ToString();

        //                //Response.Write(TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].Value);
        //                //Response.Write(TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].Text);
        //            }
        //        }
        //    }
        //}
        //TreeView1.ExpandDepth = 3;//前台设置为准
        
        //TreeView1.Nodes[0].ChildNodes[1].ChildNodes[3].ChildNodes[0].Selected = true;

        dtArea.Clear();
        dtArea = null;
        dtCountry.Clear();
        dtCountry = null;
        dtCity.Clear();
        dtCity = null;
    }

    //指定url
    private DataTable TreeURLSet(DataTable dt, string urlKey,string urlValue)
    {
        DataColumn dc = new DataColumn("treeURL");
        dt.Columns.Add(dc);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["treeURL"] = urlKey + "=" + dt.Rows[i][urlValue].ToString();
        }

        return dt;
    }

    protected void TreeView1_Unload(object sender, EventArgs e)//6
    {
        
    }
    protected void TreeView1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < TreeView1.Nodes.Count; i++)//1个--基本资料区域
        {
            TreeView1.Nodes[i].NavigateUrl = "~/BaseInfo/Area";
            for (int j = 0; j < TreeView1.Nodes[i].ChildNodes.Count; j++)//地区
            {
                TreeView1.Nodes[i].ChildNodes[j].NavigateUrl = "~/BaseInfo/" + dtArea.Rows[j]["treeURL"].ToString();
                for (int m = 0; m < TreeView1.Nodes[i].ChildNodes[j].ChildNodes.Count; m++)//国家
                {
                    TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].NavigateUrl = "City" + dtCountry.Rows[m]["treeURL"].ToString();
                    for (int n = 0; n < TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes.Count; n++)//城市
                    {
                        //TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].NavigateUrl = "CityChildHaveNoOperate" + dtCity.Rows[n]["treeURL"].ToString();

                        TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].NavigateUrl = "CityChild--DiJieShe" + dtCity.Rows[n]["treeURL"].ToString();
                        TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[1].NavigateUrl = "CityChild--JingDianZiLiao" + dtCity.Rows[n]["treeURL"].ToString();
                        TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[2].NavigateUrl = "CityChild--JiuDianZiLiao" + dtCity.Rows[n]["treeURL"].ToString();
                        TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[3].NavigateUrl = "CityChild--KeHuZiLiao" + dtCity.Rows[n]["treeURL"].ToString();

                        //Response.Write(TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].Value);
                        //Response.Write(TreeView1.Nodes[i].ChildNodes[j].ChildNodes[m].ChildNodes[n].ChildNodes[0].Text);
                    }
                }
            }
        }
    }

    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)//2
    {
        TreeNode tn = (TreeNode)sender;
        tn.NavigateUrl = "ddddddddd";
    }
}



//protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)//2
//{

//}
//protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)//3
//{

//}
//protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
//{

//}
//protected void TreeView1_Disposed(object sender, EventArgs e)
//{

//}
//protected void TreeView1_Init(object sender, EventArgs e)//1
//{

//}
//protected void TreeView1_Load(object sender, EventArgs e)//4
//{

//}
//protected void TreeView1_PreRender(object sender, EventArgs e)//5
//{

//}
//protected void TreeView1_Unload(object sender, EventArgs e)//6
//{
//    //TreeView1.ExpandDepth = 0;        
//    //this.TreeView1.CollapseAll();
//}

//DataTable dtNavigationClass = navigationBll.GetNavigationClass();
//DataTable dtArea = navigationBll.GetArea();
//DataTable dtCountry = navigationBll.GetCountry();
//DataTable dtCity = navigationBll.GetCity();

//DataTable dtNavigationContent = navigationBll.GetNavigation();
//DataTable dtNavigationClass = navigationBll.GetNavigationClass();

//if (dtNavigationClass.Rows.Count > 0)
//{
//    XmlDocument xmlDoc = new XmlDocument();            
//    xmlDoc.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><navigation></navigation>");
//    XmlNode root = xmlDoc.SelectSingleNode("navigation");

//    XmlElement[] xeClass = new XmlElement[dtNavigationClass.Rows.Count];
//    for (int i = 0; i < dtNavigationClass.Rows.Count; i++)
//    {
//        xeClass[i] = xmlDoc.CreateElement(dtNavigationClass.Rows[i]["C_type"].ToString());
//        dtNavigationContent.DefaultView.Sort = "C_type";
//        DataRowView[] dtViews = dtNavigationContent.DefaultView.FindRows(new object[] { dtNavigationClass.Rows[i]["C_type"].ToString() });
//        XmlElement[] xeName = new XmlElement[dtViews.Length];
//        for (int j = 0; j < dtViews.Length; j++)
//        {
//            xeName[j] = xmlDoc.CreateElement(dtViews[j]["C_name"].ToString());
//            xeClass[i].AppendChild(xeName[j]);
//        }
//        root.AppendChild(xeClass[i]);
//    }

//    XmlDataSource1.Data = xmlDoc.InnerXml;
//    TreeView1.DataSourceID = "XmlDataSource1";
//    TreeView1.DataBind();

//    for (int i = 0; i < TreeView1.Nodes.Count; i++)
//    {
//        for (int j = 0; j < TreeView1.Nodes[i].ChildNodes.Count; j++)
//        {
//            TreeView1.Nodes[i].ChildNodes[j].NavigateUrl = "";
//        }
//    }            
//}


//TreeNode[] tnClass = new TreeNode[dtNavigationClass.Rows.Count];
//for (int i = 0; i < dtNavigationClass.Rows.Count; i++)
//{
//    tnClass[i] = xmlDoc.CreateNode("element", dtNavigationClass.Rows[i]["C_type"].ToString(), "");
//    dtNavigationContent.DefaultView.Sort = "C_type";
//    DataRowView[] dtViews = dtNavigationContent.DefaultView.FindRows(new object[] { dtNavigationClass.Rows[i]["C_type"].ToString() });
//    TreeNode[] tnName = new TreeNode[dtViews.Length];
//    for (int j = 0; j < dtViews.Length; j++)
//    {
//        tnName[j] = xmlDoc.CreateNavigator.CreateNode("element", dtViews[j]["C_name"].ToString(), "");

//        tnClass[i].AppendChild(tnName[j]);
//    }
//    root.AppendChild(tnClass[i]);
//}

//XmlElement[] xeName = new XmlElement[int.Parse(dtNavigationClass.Rows[0]["RCount"].ToString())];
//dtNavigationContent.DefaultView.Sort = "C_type";
//DataRowView[] dtViews = dtNavigationContent.DefaultView.FindRows(new object[] { dtNavigationClass.Rows[0]["C_type"].ToString() });
//for (int i = 0; i < dtViews.Length; i++)
//{
//    xeName[i] = xmlDoc.CreateElement(dtViews[i]["C_name"].ToString());
//    root.AppendChild(xeName[i]);
//}

//foreach (DataRowView myView in dtViews)
//{
//    xeName = xmlDoc.CreateElement(myView["C_name"].ToString());
//    root.AppendChild(xeName);
//}
//if (dtNavigationClass.Rows.Count > 1)
//{
//    XmlElement xe0 = xmlDoc.CreateElement(dtNavigationClass.Rows[1]["C_type"].ToString());
//    //XmlNode root0 = xmlDoc.CreateNode("documenttype", dtNavigationClass.Rows[0]["C_type"].ToString(), "");
//    xmlDoc.AppendChild(xe0);
//}


//XmlElement xeName = xmlDoc.CreateElement("UserName");

////xeName.InnerText = "username";
//root.AppendChild(xeName);

//XmlElement xeTravelAgencyNO = xmlDoc.CreateElement("TravelAgencyNO");
//xeTravelAgencyNO.InnerText = "858585";//旅行社ID(线路判别-外键)
//root.AppendChild(xeTravelAgencyNO);

//XmlElement xeCompany = xmlDoc.CreateElement("Company");
//xeCompany.InnerText = "公司名称(旅行社)";
//root.AppendChild(xeCompany);

//XmlElement xeCompanyIntro = xmlDoc.CreateElement("CompanyIntro");
//xeCompanyIntro.InnerText = "公司介绍(公司介绍不超过2000字)";
//root.AppendChild(xeCompanyIntro);

//XmlElement xeWebIntro = xmlDoc.CreateElement("WebIntro");
//xeWebIntro.InnerText = "网站简单介绍(公司简介(不超过150字),没有公司简介就取公司介绍前150字)";
//root.AppendChild(xeWebIntro);

//XmlElement xePw = xmlDoc.CreateElement("pswd");
//xePw.InnerText = "密码";
//root.AppendChild(xePw);

//XmlElement xeLinker = xmlDoc.CreateElement("Linker");
//xeLinker.InnerText = "联系人";
//root.AppendChild(xeLinker);

//XmlElement xeSex = xmlDoc.CreateElement("LinkerSex");
//xeSex.InnerText = "联系人性别";
//root.AppendChild(xeSex);

//XmlElement xeLkTel = xmlDoc.CreateElement("LinkTel");
//xeLkTel.InnerText = "联系电话";
//root.AppendChild(xeLkTel);

//XmlElement xeEmail = xmlDoc.CreateElement("LinkEmail");
//xeEmail.InnerText = "电子邮箱";
//root.AppendChild(xeEmail);

//XmlElement xeAddress = xmlDoc.CreateElement("LinkAddress");
//xeAddress.InnerText = "联系地址";
//root.AppendChild(xeAddress);

//XmlDataSource1.Data = xmlDoc.InnerXml;
//xmlDoc.Save(Server.MapPath("w2w" + "userName" + ".xml"));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值