locked
MenuItem Target Question RRS feed

  • Question

  • User-134545009 posted

    I am using the Menu control and have a question about doing something a bit outside the box with the MenuItem elements.

    The Target for many of my MenuItems is a nested frame.  Setting the Target to a frame such as an iframe on the same page is easy, but I have a further nested framework within the iframe and I can't seem to figure out how to set the Target to one of the "second level" frames.

    If there is a way, syntactically, to refer to a nested frame, that'd be great.

    As an alternative, I'm looking at using some Javascript to do the job.  The problem with this approach is that there is no Attributes collection for the MenuItem, only the Menu itself, so I can't do something like myMenuItem.Attributes.Add("onclick", "myFunction();") 

    Any ideas?

    Wednesday, February 24, 2010 4:02 PM

Answers

  • User-1364446067 posted

    Well.. in that case (dynamic creation of menu items from code behind), you'll have to use some container control for a menu item. like a panel or even a label would work.

    You can then add the onclick for that container of the menu item, and whenever the menu item will be clicked you can have your method to work on 'onclick', in which you can achieve what you desire..


    i mean you can do like this.. 


    <asp:Menu ID="menu1" runat="server" Orientation="Vertical">
                <%--you'll add items dynamically--%>
                <StaticItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" 
                    OnInit='<%# SetAttribs(Container) %>'
                    >
                        <%# Eval("Text") %>
                        -
                        <%# Eval("Value") %>
                    </asp:Panel>
                </StaticItemTemplate>
                <DynamicItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" 
                    OnInit='<%# SetAttribs(Container) %>'
                    >
                        <%# Eval("Text") %>
                        -
                        <%# Eval("Value") %>
                    </asp:Panel>
                </DynamicItemTemplate>
            </asp:Menu>




    then in your SetAttribs method in the code behind... do like this...


    public void SetAttribs(MenuItemTemplateContainer container)
            {
                ((Panel)container.Controls[1]).Attributes.Add("onclick", "alert('hit')");
             }

    And thats it... it'd work for you..



    --

    Mark as answer, if it answers you..

    --


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2010 12:29 AM

All replies

  • User-1364446067 posted

    MenuItem after rendering on client side becomes anchor tag.. and anchor tag can have the "onclick" event.

    So you might not be able to add attribuets to MenuItem directly from the code-behind, but you can write 'onclick' for it. It'd work on client side... some thing like this...


    <asp:Menu runat="server" ID="menu">
         <Items>
            <asp:MenuItem Text="" Value="" onclick="SetTarget()"  ></asp:MenuItem>
         </Items>
        </asp:Menu>


    for accessing Iframes using javascripts see this..

    http://pietschsoft.com/post/2004/08/JavaScript-How-to-get-value-from-nested-form-in-iframe.aspx

    http://www.devarticles.com/c/a/JavaScript/Working-with-IFRAME-in-JavaScript/


    --

    Mark as answer, if it answers you..

    --



    Thursday, February 25, 2010 6:16 AM
  • User-134545009 posted

    Thanks for your response.

    That will indeed work for declarative menu items, but that does not address the vast majority of my situation, where I am dynamically creating menus and menu items.  Do you have an idea on how to deal with this dynamically?

    Thanks! 

    Thursday, February 25, 2010 8:56 AM
  • User-1364446067 posted

    Well.. in that case (dynamic creation of menu items from code behind), you'll have to use some container control for a menu item. like a panel or even a label would work.

    You can then add the onclick for that container of the menu item, and whenever the menu item will be clicked you can have your method to work on 'onclick', in which you can achieve what you desire..


    i mean you can do like this.. 


    <asp:Menu ID="menu1" runat="server" Orientation="Vertical">
                <%--you'll add items dynamically--%>
                <StaticItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" 
                    OnInit='<%# SetAttribs(Container) %>'
                    >
                        <%# Eval("Text") %>
                        -
                        <%# Eval("Value") %>
                    </asp:Panel>
                </StaticItemTemplate>
                <DynamicItemTemplate>
                    <asp:Panel ID="Panel1" runat="server" 
                    OnInit='<%# SetAttribs(Container) %>'
                    >
                        <%# Eval("Text") %>
                        -
                        <%# Eval("Value") %>
                    </asp:Panel>
                </DynamicItemTemplate>
            </asp:Menu>




    then in your SetAttribs method in the code behind... do like this...


    public void SetAttribs(MenuItemTemplateContainer container)
            {
                ((Panel)container.Controls[1]).Attributes.Add("onclick", "alert('hit')");
             }

    And thats it... it'd work for you..



    --

    Mark as answer, if it answers you..

    --


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2010 12:29 AM