MSDN Sample AccessSiteMapProvider 不能使TreeView控件工作的解决

来源:互联网 发布:谷歌娘配音软件 编辑:程序博客网 时间:2024/05/18 03:12

在.Net 2.0环境下使用TreeView控件,我想自定义SiteMapProvider。查阅MSDN,有一个例子AccessSiteMapProvider,ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_aspnetcon/html/636e72a1-8bbf-4b1f-bb52-7429c4c90e01.htm

但是该例子有问题,TreeView控件只显示根节点,Menu和SiteMapPath控件可以工作。

检查后发现是BuildSiteMap函数中没有向Provider添加根节点,修改如下:

        public override SiteMapNode BuildSiteMap()
        {

            // Since the SiteMap class is static, make sure that it is
            // not modified while the site map is built.
            lock (this)
            {

                // If there is no initialization, this method is being
                // called out of order.
                if (!IsInitialized)
                {
                    throw new Exception("BuildSiteMap called incorrectly.");
                }

                // If there is no root node, then there is no site map.
                if (null == rootNode)
                {
                    // Start with a clean slate
                    Clear();

                    // Select the root node of the site map from Microsoft Access.
                    int rootNodeId = -1;

                    if (accessConnection.State == ConnectionState.Closed)
                        accessConnection.Open();
                    OleDbCommand rootNodeCommand =
                        new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
                                         accessConnection);
                    OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();

                    if (rootNodeReader.HasRows)
                    {
                        rootNodeReader.Read();
                        rootNodeId = rootNodeReader.GetInt32(0);
                        // Create a SiteMapNode that references the current StaticSiteMapProvider.
                        rootNode = new SiteMapNode(this,
                                                     rootNodeId.ToString(),
                                                     rootNodeReader.GetString(1),
                                                     rootNodeReader.GetString(2));
                    }
                    else
                    {
                        rootNodeReader.Close();
                        accessConnection.Close();
                        return null;
                    }

                    // Add the node to the site map
                    AddNode(rootNode, null);

                    rootNodeReader.Close();
                    // Select the child nodes of the root node.
                    OleDbCommand childNodesCommand =
                        new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?",
                                         accessConnection);
                    OleDbParameter rootParam = new OleDbParameter("parentid", OleDbType.Integer);
                    rootParam.Value = rootNodeId;
                    childNodesCommand.Parameters.Add(rootParam);

                    OleDbDataReader childNodesReader = childNodesCommand.ExecuteReader();

                    if (childNodesReader.HasRows)
                    {

                        SiteMapNode childNode = null;
                        while (childNodesReader.Read())
                        {
                            childNode = new SiteMapNode(this,
                                                         childNodesReader.GetInt32(0).ToString(),
                                                         childNodesReader.GetString(1),
                                                         childNodesReader.GetString(2));

                            // Use the SiteMapNode AddNode method to add
                            // the SiteMapNode to the ChildNodes collection.
                            AddNode(childNode, rootNode);
                        }
                    }

                    childNodesReader.Close();
                    accessConnection.Close();
                }
                return rootNode;
            }
        }

原创粉丝点击