如何在 ASP.NET 中创建菜单栏

本文关键字:创建 菜单栏 NET ASP | 更新日期: 2023-09-27 17:56:14

我必须为我的 Web 应用程序创建一个菜单栏。我不知道如何创建它。我遇到了几个网站并下载了一些示例代码。我为此创建了一个母版页,并粘贴了下面的代码来创建我使用的菜单。

 [MethodImpl(MethodImplOptions.Synchronized)]
public override SiteMapNode BuildSiteMap()
{
    // Return immediately if this method has been called before
    if (_root != null)
        return _root;
    // Create a dictionary for temporary node storage and lookup
    Dictionary<int, SiteMapNode> nodes = new Dictionary<int, SiteMapNode> (16);

    // Query the database for site map nodes
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["SQLConnectionString"]))
        {
        connection.Open();
        SqlCommand command = new SqlCommand("SELECT ID, Title, Description, Url, Roles, Parent FROM ven_sitemap ORDER BY ID", connection);
        SqlDataReader reader = command.ExecuteReader ();
        int id = reader.GetOrdinal("ID");
        int url = reader.GetOrdinal ("Url");
        int title = reader.GetOrdinal ("Title");
        int desc = reader.GetOrdinal("Description");
        int roles = reader.GetOrdinal ("Roles");
        int parent = reader.GetOrdinal("Parent");
        while (reader.Read())
        {
            // Create the root SiteMapNode
            // Build a tree of SiteMapNodes underneath the root node
            //while (reader.Read())
            //{
            if (reader["parent"].ToString() == "0")
            {
                _root = new SiteMapNode(this, reader.GetInt32(id).ToString(), reader.IsDBNull(url) ? null : reader.GetString(url),
                    reader.GetString(title), reader.IsDBNull(desc) ? null : reader.GetString(desc));

                if (!reader.IsDBNull(roles))
                {
                    string rolenames = reader.GetString(roles).Trim();
                    if (!String.IsNullOrEmpty(rolenames))
                    {
                        string[] rolelist = rolenames.Split(new char[] { ',', ';' }, 512);
                        _root.Roles = rolelist;
                    }
                }
                //  Add "*" to the roles list if no roles are specified
                if (_root.Roles == null)
                    _root.Roles = new string[] { "*" };
                // Record the root node in the dictionary
                if (nodes.ContainsKey(reader.GetInt32(id)))
                    throw new ConfigurationErrorsException(_errmsg2); // ConfigurationException pre-Beta 2
                nodes.Add(reader.GetInt32(id), _root);
                // Add the node to the site map
                AddNode(_root, null);
            }
            else
                {
                    SiteMapNode node = new SiteMapNode(this, reader.GetInt32(id).ToString(), reader.IsDBNull(url) ? null : reader.GetString(url),
                        reader.GetString(title), reader.IsDBNull(desc) ? null : reader.GetString(desc));
                    if (!reader.IsDBNull(roles))
                    {
                        string rolenames = reader.GetString(roles).Trim();
                        if (!String.IsNullOrEmpty(rolenames))
                        {
                            string[] rolelist = rolenames.Split(new char[] { ',', ';' }, 512);
                            node.Roles = rolelist;
                        }
                    }
                    // If the node lacks roles information, "inherit" that
                    // information from its parent
                    SiteMapNode parentnode = nodes[reader.GetInt32(parent)];
                    if (node.Roles == null)
                        node.Roles = parentnode.Roles;

                    // Record the node in the dictionary
                    if (nodes.ContainsKey(reader.GetInt32(id)))
                        throw new ConfigurationErrorsException(_errmsg2);
                    nodes.Add(reader.GetInt32(id), node);
                    // Add the node to the site map
                    AddNode(node, parentnode);
                }
            //}
        }
    }
    // Return the root SiteMapNode
    return _root;

}
protected override SiteMapNode GetRootNodeCore ()
{
    BuildSiteMap ();
    return _root;
}

我的桌子 :

ID  Title   Description          Url                   Roles    Parent
 1  HOME    NULL    ~/Reports/Production_data_report.aspx   2   0
 2  Machinename NULL    ~/Reports/machine_name.aspx         3     1
 3  Business Quote  NULL    ~/Reports/business_quote.aspx   2   1
 6  Machine Counter NULL    ~/Reports/machine_counter.aspx  1   0
 7  Data Query  NULL    ~/Reports/data_query_page.aspx   2  6
 8  Production Report   NULL    ~/Reports/yoneda_report.aspx    2   6

代码输出:

Machine counter
  Data query
  Production Report

但是我需要这样的输出,

  Home             Machine counter
  machinename        Data query
  businessquote       Production Report

其中主节点和计算机计数器是根节点。当我执行上面的代码时,我的第一个根节点被第二个根节点替换。

请帮我解决这个问题。

如何在 ASP.NET 中创建菜单栏

除非您特别需要从数据库中提取站点结构,否则我建议您将 ASP.NET 站点地图与Web.sitemap文件一起使用,然后将其与 ASP.NET 菜单控件连接。你的问题有点不清楚,所以如果这不是你所追求的,请道歉。

我使用CSS和HTML来满足我的要求..我已经删除了我所有的服务器端编码。

编码:

  <ul id="topnav" style="left: 24px; width: 95%; position: absolute; top: 86px">
    <li class ="admin" style="background-color:#FF9900; left: 0px; top: 0px;">
        <h4>
            &nbsp; &nbsp;&nbsp; ADMIN &nbsp; &nbsp; 
        </h4>
         <div class="sub">
            <ul>
                <li><h2><a href="#">ADD MACHINE</a></h2></li>
                <li><h2><a href="#">ADD JOB</a></h2></li>
            </ul>
         </div>
    </li>
    <li class ="report" style="background-color:#ff9933; left: 154px; top: 0px;">
        <h4 style="background-color: #ff9933">
            &nbsp;&nbsp; REPORT &nbsp; &nbsp; &nbsp;
        </h4>
        <div class="sub">
            <ul>
                <li style ="color:Red;"><h2>MACHINE REPORT</h2></li>
                <li style="visibility:hidden;"><a href="#">PRODUCTION REPORT</a></li>
            </ul>
            <ul>
                <li style ="color:Red;"><h2>TIME SHEET</h2></li>
                <li ><a href="#">ASSEMBLY</a></li>
                <li><a href="#">MAINTENANCE</a></li>
                <li><a href="#">CNC</a></li>
                <li><a href="#">DESIGN</a></li>
            </ul>
            <ul>
                <li style ="color:Red;"><h2>MACHINE COUNTER</h2></li>
                <li><a href="#">MACHINE COUNTER</a></li>
            </ul>
        </div>
    </li>
    <li class ="business" style="left: -100px; top: 0px; height: 38px; background-color: #ff9933" >
        <h4>
            &nbsp;&nbsp; BUSINESS &nbsp;
        </h4>
         <div class="sub">
            <ul>
                <li><h2><a href="#">BUSINESS QOUTE</a></h2></li>
                <li><h2><a href="#">BUSINESS AWARD </a></h2></li>
            </ul>
         </div>
     <!--
    <li>
        Store Locator
    </li>