asp.net menu with submenu items, redirect by code behind

巧了我就是萌 提交于 2019-12-22 08:05:38

问题


I have a menu control with 2 levels of menu items! I would like to navigate via code behind OnMenuItemClick event because when i use NavigationUrl property my ajax UpdateProgress wont start!

The Problem is, whatever submenu item i click, when the event raises it gets every time the first item?!

<asp:Menu ID="Menu1" runat="server" onmenuitemclick="Menu1_MenuItemClick" >
...
    <Items>
    <asp:MenuItem Text="WorkFlows">
    <asp:MenuItem Text="Overview" Value="Workflow"></asp:MenuItem>
    <asp:MenuItem Text="Edit" Value="Workflow"></asp:MenuItem>
    <asp:MenuItem Text="Create" Value="Workflow"></asp:MenuItem>
    <asp:MenuItem Text="Delete" Value="Workflow"></asp:MenuItem>
    </asp:MenuItem>

    <asp:MenuItem Text="HoursOfBusiness">
    <asp:MenuItem Text="Overview" Value="HoursOfBusiness"></asp:MenuItem>
    <asp:MenuItem Text="Edit" Value="HoursOfBusiness"></asp:MenuItem>
    <asp:MenuItem Text="Create" Value="HoursOfBusiness"></asp:MenuItem>
    <asp:MenuItem Text="Delete" Value="HoursOfBusiness"></asp:MenuItem>
    </asp:MenuItem>


    </Items>
    </asp:Menu>

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {

            switch (e.Item.Value)
            {
                case "Workflow":
                    switch (e.Item.Text)
                    {
                        case "Overview":
                            Response.Redirect("~/RGS/Workflow/Workflow.aspx");
                            break;
                            case "Edit":
                            Response.Redirect("~/RGS/Workflow/WorkflowEdit.aspx");
                            break;
                            case "Create":
                            Response.Redirect("~/RGS/Workflow/WorkflowCreate.aspx");
                            break;
                            case "Delete":
                            Response.Redirect("~/RGS/Workflow/WorkflowDelete.aspx");
                            break;
                    }
                    break;
                case "HoursOfBusiness":
                    switch (e.Item.Text)
                    {
                        case "Overview":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                        case "Edit":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                        case "Create":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHoursCreate.aspx");
                            break;
                        case "Delete":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                    }
                    break;
            }

so when i click Create under HoursOfBusiness i get the Overview menuItem send to the event?! I hope the explanation is understandable?! :-)


回答1:


This is how I solved this problem:

<asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick">
    <Items>
        <asp:MenuItem Text="WorkFlows">
            <asp:MenuItem Text="Overview" />
            <asp:MenuItem Text="Edit" />
            <asp:MenuItem Text="Create" />
            <asp:MenuItem Text="Delete" />
        </asp:MenuItem>
        <asp:MenuItem Text="HoursOfBusiness">
            <asp:MenuItem Text="Overview" />
            <asp:MenuItem Text="Edit" />
            <asp:MenuItem Text="Create" />
            <asp:MenuItem Text="Delete" />
        </asp:MenuItem>
    </Items>
</asp:Menu>

And the code behind is like this:

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
    bool hasParent = (e.Item.Parent != null);

    switch (hasParent)
    {
        case false:
            switch (e.Item.Value)
            {
                case "WorkFlows":
                    Response.Redirect("~/RGS/Workflow/Workflow.aspx");
                    break;
                case "HoursOfBusiness":
                    Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                    break;
            }
            break;
        case true:
            switch (e.Item.Parent.Value)
            {
                case "WorkFlows":
                    switch (e.Item.Value)
                    {
                        case "Overview":
                            Response.Redirect("~/RGS/Workflow/Workflow.aspx");
                            break;
                        case "Edit":
                            Response.Redirect("~/RGS/Workflow/WorkflowEdit.aspx");
                            break;
                        case "Create":
                            Response.Redirect("~/RGS/Workflow/WorkflowCreate.aspx");
                            break;
                        case "Delete":
                            Response.Redirect("~/RGS/Workflow/WorkflowDelete.aspx");
                            break;
                    }
                    break;
                case "HoursOfBusiness":
                    switch (e.Item.Value)
                    {
                        case "Overview":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                        case "Edit":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                        case "Create":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHoursCreate.aspx");
                            break;
                        case "Delete":
                            Response.Redirect("~/RGS/Workflow/BusinessHour/BusinessHours.aspx");
                            break;
                    }
                    break;
            }
            break;

    }
}

Hopefully this will help you solve your problem.



来源:https://stackoverflow.com/questions/7711891/asp-net-menu-with-submenu-items-redirect-by-code-behind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!