繁体   English   中英

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

[英]How to create menu bar in ASP.NET

我必须为我的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

其中home和machine counter是根节点。 当我执行以上代码时,我的第一个根节点被第二个根节点替换。

请帮我解决这个问题。

除非您特别需要从数据库中提取站点结构,否则建议将ASP.NET Site MapsWeb.sitemap文件一起使用,然后将其与ASP.NET Menu Control连接起来。 您的问题尚不清楚,因此,如果不是您所追求的,我们深表歉意。

我已经使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM