如何为asp中的子菜单和菜单编写onclick函数

时间:2014-11-13 05:55:02

标签: javascript asp.net menu submenu

我搜索了Google,我没有得到解决方案我的要求, 我的输出像, enter image description here

如果我单击“主页”和“许可证”和“报告”表示我想重定向另一个Aspx页面。怎么做?  编码背后的代码

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim cmd As New SqlCommand
            Dim data As New SqlDataAdapter
            Dim ds As New DataSet
            Dim strcon As String = ConfigurationManager.ConnectionStrings("aaaaa").ConnectionString
            Dim con As New SqlConnection(strcon)
            Dim da As New SqlDataAdapter("Select MenuID, Menus from MenusDetails", con)
            Dim dttc As New DataTable()
            da.Fill(dttc)
            Dim main As HtmlGenericControl = UList("Menuid", "menu")
            Dim dtDist As New DataTable()
            For Each row As DataRow In dttc.Rows
                'For i As Integer = 0 To dttc.Rows.Count - 1
                'dtDist.Columns.Clear()
                cmd.Parameters.Clear()
                dtDist.Clear()
                cmd.CommandText = "FindMenus"
                cmd.Connection = con
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@MenuId", SqlDbType.Int).Value = row(0).ToString()
                data.SelectCommand = cmd
                data.Fill(dtDist)
                If dtDist.Rows.Count > 0 Then
                Dim sub_menu As HtmlGenericControl 
                sub_menu = LIList(row("Menus").ToString(), row("MenuID").ToString())
                Dim ul As New HtmlGenericControl("ul")
                Dim mains As HtmlGenericControl = UList("SubMenuId", "menu")
                For Each r As DataRow In dtDist.Rows
                ul.Controls.Add(LIList(r("SubMenu").ToString(), r("SubMenuId").ToString()))
                Dim ul1 As New HtmlGenericControl("ul")
                sub_menu.Controls.Add(ul)
                main.Controls.Add(sub_menu)
                 Next
                 Else
               If row("Menus") = "Home" Or row("Menus") = "Report" Then
                main.Controls.Add(LIList(row("Menus").ToString(), row("Menuid").ToString()))
                 Else
               End If
          End If
            Next
            Panel1.Controls.Add(main)
        End Sub
        Private Function UList(ByVal id As String, ByVal cssClass As String) As HtmlGenericControl
            Dim ul As New HtmlGenericControl("ul")
            ul.ID = id
            ul.Attributes.Add("class", cssClass)
            Return ul
        End Function
        Private Function LIList(ByVal innerHtml As String, ByVal rel As String) As HtmlGenericControl
            Dim li As New HtmlGenericControl("li")
            li.Attributes.Add("rel", rel)
            li.InnerHtml = (Convert.ToString("<a href=" + ">") & innerHtml) + "</a>"
            Return li
 End Function

My Aspx Page Like

 <div id="menu" class="menu" align="center" >
        <asp:Panel ID="Panel1" Width ="800px" runat="server">
        </asp:Panel>
    </div>

1 个答案:

答案 0 :(得分:0)

不是按HtmlGenericControl创建菜单,而是直接使用<asp:Menu..控件。

<asp:Menu ID="muWeb" runat="server" Orientation="Horizontal" OnMenuItemClick="muWeb_MenuItemClick">  </asp:Menu>

这已经有一个OnMenuItemClick事件处理程序。

在从DB构建菜单时,需要使用C#代码构建数据源,如下所示。我已经展示了使用集合..您可以根据自己的方便将其更改为数据。

private void BuildMenu()
        {
            // get resultMenu from DB.
            foreach (MenuDTO menuDTO in resultMenu.Items)
            {
                if (menuDTO.ParentMenuId == null)
                {
                    MenuItem miParent = new MenuItem();
                    miParent.Text = menuDTO.MenuName;
                    miParent.NavigateUrl = menuDTO.URL;
                    BuildChildMenu(miParent, menuDTO, resultMenu);
                    muWEB.Items.Add(miParent);
                }
            }

        }

        private void BuildChildMenu(MenuItem piMenuItem, MenuDTO currentDTO, ResultSetDTO<MenuDTO> sourceDTO)
        {
            foreach (MenuDTO menuDTO in sourceDTO.Items.Where(operand => operand.ParentMenuId == currentDTO.MenuId).ToList<MenuDTO>())
            {
                MenuItem miChild = new MenuItem();
                miChild.Text = menuDTO.MenuName;
                miChild.NavigateUrl = menuDTO.MenuName;
                // Make recursive calll untill all levels are done.
                BuildChildMenu(miChild, menuDTO, sourceDTO);
                piMenuItem.ChildItems.Add(miChild);
            }
        }