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

翻译|其它|编辑:郝浩|2007-08-31 09:28:15.000|阅读 1463 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

.Net 2.0环境下使用  TreeView  控件,我想自定义  SiteMapProvider。查阅  MSDN,有一个例子  AccessSiteMapProviderms-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;
            }
        }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP