简体   繁体   中英

ASP.Net Menu Bar is not being displayed

I built a Menu Bar to my site master in ASP.net:

<div class="MenuBar">
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
            <asp:Menu ID="menuBar" runat="server" Orientation="Vertical" Width="100%">
                <DynamicHoverStyle CssClass="DynamicHover" />
                <DynamicMenuItemStyle CssClass="DynamicMenuItem" />
                <DynamicSelectedStyle CssClass="DynamicHover" />
                <StaticHoverStyle CssClass="staticHover" />
                <StaticMenuItemStyle CssClass="StaticMenuItem" ItemSpacing="1px" />
                <StaticSelectedStyle CssClass="staticHover" />
            </asp:Menu>
        </asp:ContentPlaceHolder>
    </div>

Code behind:

public partial class SiteMaster : System.Web.UI.MasterPage
{

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        getMenu();
    }
}


private void getMenu()
{
    Menu menuBar = new Menu();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
    con.Open();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string sql = "Select [Material Name] from Materials";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(ds);
    dt = ds.Tables[0];
    DataRow[] drowpar = dt.Select();
    String s = "sss";
    foreach (DataRow dr in drowpar)
    {
        menuBar.Items.Add(new MenuItem(dr["Material Name"].ToString()));
    }

    con.Close();

}

}

For some reason the menu is not being displayed when I view the site on the browser. Someone knows why? Thanks.

You instantiate a new object from menu then don't equel it to the exist menu, You don't need to instantiate a new one, just replace you function to below :

    private void getMenu()
{
   // Menu menuBar = new Menu();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
    con.Open();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string sql = "Select [Material Name] from Materials";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(ds);
    dt = ds.Tables[0];
    DataRow[] drowpar = dt.Select();
    String s = "sss";
    foreach (DataRow dr in drowpar)
    {
        menuBar.Items.Add(new MenuItem(dr["Material Name"].ToString()));
    }

    con.Close();

}
}

And call it from Page_PreRender and not from Page_Load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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