When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

Last post 06-09-2009 8:14 PM by pfontyn. 19 replies.

Sort Posts:

  • When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-01-2006, 4:40 PM
    • Member
      85 point Member
    • DragonNinja
    • Member since 11-04-2002, 10:11 AM
    • Posts 17
    First of all... I must say "Very good Job guys!!!", this Adapters are great!
    In the standard Menu control (without the CSS Friendly Adapter) when we setup menu items without setting the NavigateUrl property, the normal behavior is that at runtime clicking on menu item will fire the MenuItemClick server event.

    Using the new CSSFriendly adapter, and leaving null the NavigateUrl property of the menu items, no server event is raised!

    IMO this is an incoherent behavior.... do you agree?
    Hot to fix this?

    Thks.

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-03-2006, 5:41 AM
    • Member
      85 point Member
    • DragonNinja
    • Member since 11-04-2002, 10:11 AM
    • Posts 17
    Ok, I've figured on my own hacking the MenuAdapter class, I tried as follow:
            private void BuildItems(MenuItemCollection items, bool isRoot, HtmlTextWriter writer)
            {
                if (items.Count > 0)
                {
                    writer.WriteLine();

                    writer.WriteBeginTag("ul");
                    if (isRoot)
                    {
                        writer.WriteAttribute("class", "AspNet-Menu");
                    }
                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.Indent++;
                                  
                    foreach (MenuItem item in items)
                    {
                        BuildItem(item, writer);
                    }

                    writer.Indent--;
                    writer.WriteLine();
                    writer.WriteEndTag("ul");
                }
            }

            private void BuildItem(MenuItem item, HtmlTextWriter writer)
            {
                Menu menu = Control as Menu;
                if ((menu != null) && (item != null) && (writer != null))
                {
                    writer.WriteLine();
                    writer.WriteBeginTag("li");
                    writer.WriteAttribute("class", item.ChildItems.Count > 0 ? "AspNet-Menu-WithChildren" : "AspNet-Menu-Leaf");
                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.Indent++;
                    writer.WriteLine();

                    if (item.NavigateUrl.Length > 0)
                    {
                        writer.WriteBeginTag("a");
                        writer.WriteAttribute("href", Page.ResolveUrl(item.NavigateUrl));
                        writer.WriteAttribute("class", "AspNet-Menu-Link");
                        if (item.Target.Length > 0)
                        {
                            writer.WriteAttribute("target", item.Target);
                        }
                        if (item.ToolTip.Length > 0)
                        {
                            writer.WriteAttribute("title", item.ToolTip);
                        }
                        else if (menu.ToolTip.Length > 0)
                        {
                            writer.WriteAttribute("title", menu.ToolTip);
                        }
                        writer.Write(HtmlTextWriter.TagRightChar);
                        writer.Indent++;
                        writer.WriteLine();
                    }
                    else
                    {
                       if (item.Selectable)
                       {
                          writer.WriteBeginTag("a");
                          PostBackOptions options = new PostBackOptions(menu);
                          options.ActionUrl = HttpContext.Current.Request.Url.ToString();
                          options.Argument = item.ValuePath;
                          options.AutoPostBack = false;
                          options.RequiresJavaScriptProtocol = true;
                          options.PerformValidation = true;

                          writer.WriteAttribute("href", Page.ClientScript.GetPostBackEventReference(options));

                          writer.WriteAttribute("class", "AspNet-Menu-Link");
                          if (item.Target.Length > 0)
                          {
                             writer.WriteAttribute("target", item.Target);
                          }
                          if (item.ToolTip.Length > 0)
                          {
                             writer.WriteAttribute("title", item.ToolTip);
                          }
                          else if (menu.ToolTip.Length > 0)
                          {
                             writer.WriteAttribute("title", menu.ToolTip);
                          }
                          writer.Write(HtmlTextWriter.TagRightChar);
                          writer.Indent++;
                          writer.WriteLine();
                       }
                       else
                       {
                          writer.WriteBeginTag("span");
                          writer.WriteAttribute("class", "AspNet-Menu-NonLink");
                          writer.Write(HtmlTextWriter.TagRightChar);
                          writer.Indent++;
                          writer.WriteLine();
                       }
                    }

                    if (item.ImageUrl.Length > 0)
                    {
                        writer.WriteBeginTag("img");
                        writer.WriteAttribute("src", Page.ResolveUrl(item.ImageUrl));
                        writer.WriteAttribute("alt", item.ToolTip.Length > 0 ? item.ToolTip : (menu.ToolTip.Length > 0 ? menu.ToolTip : item.Text));
                        writer.Write(HtmlTextWriter.SelfClosingTagEnd);
                    }

                    writer.Write(item.Text);

                    if (item.NavigateUrl.Length > 0)
                    {
                        writer.Indent--;
                        writer.WriteLine();
                        writer.WriteEndTag("a");
                    }
                    else
                    {
                        writer.Indent--;
                        writer.WriteLine();
                        writer.WriteEndTag("span");
                    }

                    if ((item.ChildItems != null) && (item.ChildItems.Count > 0))
                    {
                        BuildItems(item.ChildItems, false, writer);
                    }

                    writer.Indent--;
                    writer.WriteLine();
                    writer.WriteEndTag("li");
                }

    Now, apart a little problem with css, the menu items will do a postpack, but I can't hadle it in the MenuItemClick event, seems it ins't fired correctly...

    Please, anyone can tell me the correct way to proceed?
    Thks
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-09-2006, 11:16 AM
    • Member
      7 point Member
    • jamfahr
    • Member since 05-09-2006, 3:08 PM
    • Posts 2

    You have a couple of problems. The primary one is that the controladapter receives the postback now, not the menu control. By default it will try and find the correct menu item and raise the onclick event. However, it expects the path to the menu item to be seperated by "\" rather than the pathseperator (normally "/"). In addition, it expects the item name to be prepended with the letter "b". Both of these are undocumented. So, what I did was use the following

    writer.WriteAttribute(

    "href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" +  item.ValuePath.Replace(menu.PathSeparator, "\"c), True));

    Finally, at the end of the method you need to change

    if (item.NavigateUrl.Length > 0)

    to

    if (item.NavigateUrl.Length > 0 || item.Selectable)

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-24-2006, 11:13 AM
    • Member
      135 point Member
    • shazelton
    • Member since 07-07-2005, 10:43 AM
    • Southampton
    • Posts 23

    Hi

     

    Have been trying to get this to work with a pretty simple menu but can't seem to get the syntax right from your post.  Is there a typo in there?

     

    the "/"c seems to cause the issue.

     

    Simon

    Great work by the way!

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-24-2006, 12:57 PM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    I've been looking at this, too.  Here's the code that I ended up putting into my evolving (not yet released) new version of the MenuAdapter (for C#):

     

                        if ((item.NavigateUrl.Length > 0) || item.Selectable)
                        {
                            writer.WriteBeginTag("a");
                            if (item.NavigateUrl.Length > 0)
                            {
                                writer.WriteAttribute("href", Page.Server.HtmlEncode(Page.ResolveUrl(item.NavigateUrl)));
                            }
                            else
                            {
                                writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" +  item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));
                            }
                            writer.WriteAttribute("class", "AspNet-Menu-Link");
    

    You'll also want to change the "if" that closes the "a" tag so it includes both the check for the NavigateUrl length and item.Selectable like this:

                        if ((item.NavigateUrl.Length > 0) || item.Selectable)
                        {
                            writer.Indent--;
                            writer.WriteLine();
                            writer.WriteEndTag("a");
                        }
                        else
                        {
                            writer.Indent--;
                            writer.WriteLine();
                            writer.WriteEndTag("span");
                        }
    
    Russ Helfand
    Groovybits.com
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-24-2006, 1:56 PM
    • Member
      135 point Member
    • shazelton
    • Member since 07-07-2005, 10:43 AM
    • Southampton
    • Posts 23
    Awesome, that's really helpful, will give some feedback on how it goes with our project soon!
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    05-24-2006, 2:07 PM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    By the way, here is a test page you can use once your enhanced your MenuAdapter:

     

    <%@ Page Language="C#" StylesheetTheme="Basic" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
      void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
      {
        // Display the text of the menu item selected by the user.
        Message.Text = "You selected " + 
          e.Item.Text + ".";
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <!--[if lt IE 9]>
        <link runat="server" rel="stylesheet" href="~/BrowserSpecificCSS/IEMenu.css" type="text/css" id="IEMenuCSS" />
    <![endif]--> 
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="SampleMenu">
          <asp:menu id="NavigationMenu" onmenuitemclick="NavigationMenu_MenuItemClick" runat="server" SkinID="SampleMenuVertical" CssSelectorClass="PrettyMenu">      
            <items>
              <asp:menuitem text="Home">
                <asp:menuitem text="Music">
                  <asp:menuitem text="Classical" />
                  <asp:menuitem text="Rock" />
                  <asp:menuitem text="Jazz" />
                </asp:menuitem>
                <asp:menuitem text="Movies" Selectable="false">
                  <asp:menuitem text="Action" />
                  <asp:menuitem text="Drama" />
                  <asp:menuitem text="Musical" />
                </asp:menuitem>
              </asp:menuitem>
            </items>
          </asp:menu>    
        </div>
        
        <asp:label id="Message" runat="server"/>
        
        </form>
    </body>
    </html>
    
    

    Put this page in a site created from the CSS Adapter web template (in VWD) and run it.  When you hit the selectable nodes in the menu you should see the page postback and report what you clicked on.

    Russ Helfand
    Groovybits.com
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    06-27-2006, 10:31 AM
    • Member
      470 point Member
    • mighty_man
    • Member since 07-15-2002, 4:59 PM
    • Posts 103

    When I changed the adapter code and check the demo code all worked fine.

    But whem implementing the same adapter on my own code, no postback.

    The page for the implementation has two menues, one with NavigationUrl and one without. The one without is using values on each node. So without the adapter the postback argument is the value.

    When the changed adapter code didn´t work, I tried to change the postback with the argument of item.value instead of b+ ..... :)

    So the postback url seems exaktly like it was before using the adapter, but no luck.

    I don´t fullt get the b prefix thing. Has this code change since this post ?

     

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    06-27-2006, 11:20 AM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    Can you post a test page that demonstrates your situation?

    Russ Helfand
    Groovybits.com
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    07-10-2006, 9:23 AM
    • Member
      45 point Member
    • Shakhan
    • Member since 07-03-2006, 8:51 AM
    • Posts 9

    Hello, I'm having quite the same problem...

    I got a menu which use of course the adapter, and use a sitemapdatasource,

    I would like a second Menu control to display elements of the same level of the one clicked on the first menu. If I am not clear have a look at http://www.eurosport.com/

     I found a note from Micorsoft about that : http://msdn2.microsoft.com/en-us/library/16yk5dby.aspx but there example suppose the application to raise the MenuItemClick event... easy for them as they don't set url properties for their siteMapNode.

    NB: the MenuItemClick event is raised when no navigateUrl is set !

    So I tried to override the MenuItem class to add my own attribute but unfortunately this class is sealed and can't be inherited. http://forums.asp.net/thread/1087572.aspx

    I tried to hook this by setting navigateUrl to an empty string programaticaly according to : http://www.codecomments.com/archive320-2006-1-780129.html

    and passing the url by the tooltip as I couldn't find any other property

    protected

    void MyMenu_MenuItemDataBound(object sender, MenuEventArgs e){

    e.Item.ToolTip = e.Item.NavigateUrl;

    e.Item.NavigateUrl =

    null;// I also tried e.Item.NavigateUrl = ""}

    And in the adapter after putting in comment the line : //writer.WriteAttribute("href",

    I add the onclick attribute

    writer.WriteAttribute("OnClick", Page.ClientScript.GetPostBackEventReference(this.Control, item.ToolTip, true));

    There is the generated code is :

    <li class="AspNet-Menu-Leaf">
          <a class="AspNet-Menu-Link" OnClick="__doPostBack('MyMenu','/folder/myPage.aspx?culture=fr-FR')" >
           Ma Page
          </a>
    </li>

    When I click on the menuItem the postback occurs but I still don't pass in the menuItemClick event and I really don't know if i can handle the postback by myself ?

    Please tell me if I was clear and specially if I wasn't ;-)

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    07-10-2006, 2:20 PM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    Shakhan, did you modify the MenuAdapter class from the original kit so it has the logic I described in the earlier response in this thread?  I'm talking about the reply above that starts with the text:

    I've been looking at this, too.  Here's the code...

    This really should work for you.  Try making this modification and test your change with the text page that I provided above in this thread.  Make sure that works as expected.  If that is OK then try it in your real situation. Does that work, too?

    Russ Helfand
    Groovybits.com
  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    07-11-2006, 4:08 AM
    • Member
      45 point Member
    • Shakhan
    • Member since 07-03-2006, 8:51 AM
    • Posts 9

    Hi Russ! Thanks for your interest to my problem

    Yes I tried the code you mentionned, but when running a javascript error occurs telling that the function __DoPostBack was undefined.

    I decide to try it again with the test page, and I didn't even need the test page, it's works perfectly, I now pass in the menuItemClick event !!

    Thank you so much Russ for giving me back my faith in .Net ;-) 

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    01-01-2007, 9:56 PM
    • Member
      2 point Member
    • Horizon_U
    • Member since 01-02-2007, 2:33 AM
    • Posts 1

    It is not that elegant solution maybe but you can also do it this way, 

    --MasterPage  

    Create a property to make MenuItems selected

    Public WriteOnly  Property MenuItemNum() As Integer

    Set(ByVal value As Integer)

    Navigation.Items(value).Selected =

    True

    End Set

    End Property

     

    Assign  NavigateUrl for each MenuItem  

    <

    asp:Menu runat="server" ID="Navigation"  ------

    ...

    <

    asp:MenuItem Text="Report" Value="Report" NavigateUrl="~/Default3.aspx"></asp:MenuItem>

     

     Add this on your Content Pages to reach the any property of the MasterPage 

    <%@ MasterType virtualpath="~/MasterPage.master" %>

     on Content Page  , Page Load event 

    Make proper  MenuItem selected ( Master.MenuItemNum = 0.1......) 

     

     

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    04-27-2007, 12:06 PM
    • Member
      6 point Member
    • askforprem
    • Member since 04-27-2007, 2:51 PM
    • Posts 3

    Hi all,

      I have been following this thread since i have the exact same problem.Am new to .NET 2.0

    and greatly appreciate any help.

    Here's my situation

    I have a main menu which is horizontal,and am using the CSS Control adapters(menu)

    The Menu picks its items from the Web.Sitemap which is as follows

    <?xml version="1.0" encoding="utf-8" ?>

    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="~/Home.aspx" title="Home" description="Home">

    <siteMapNode title="VDM" url="~/VDM/Default.aspx" description="Basic Reporting Samples">

    </siteMapNode>

    <

    siteMapNode title="DataDashBoard" url="~/DataDashBoard/Default.aspx" description="Contains DashBoard Info">

    </

    siteMapNode>

    <

    siteMapNode title="Logout" url="~/Logout.aspx" description="Logout Page">

    </

    siteMapNode>

    </

    siteMapNode>

    </

    siteMap>

     

    The Menu in the MasterPage.aspx.cs looks like this

     <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal" CssSelectorClass="PrettyMenu" OnMenuItemClick="Menu1_MenuItemClick" Height="28px" Width="518px">

     

     

    </asp:Menu></span>

    <asp

    :SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />

     I modifed the MenuAdapter.cs file in the BuildItem() to look like this

    if

    (IsLink(item))

    {

    //New code from here

     

    //To here

    if ((item.NavigateUrl.Length > 0) || item.Selectable)

    {

    writer.WriteBeginTag(

    "a");

    if (!String.IsNullOrEmpty(item.NavigateUrl))

    {

    writer.WriteAttribute(

    "href", Page.Server.HtmlEncode(Page.ResolveUrl(item.NavigateUrl)));

    //writer.WriteAttribute("href", Page.Server.HtmlEncode(menu.ResolveClientUrl(item.NavigateUrl)));

    }

    }

    else

    {

    writer.WriteAttribute(

    "href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));

    //writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));

    }

    writer.WriteAttribute(

    "class", "AspNet-Menu-Link");

    // writer.WriteAttribute("class", GetItemClass(menu, item));

    //WebControlAdapterExtender.WriteTargetAttribute(writer, item.Target);

     And also the end of the method like this

    if ((item.NavigateUrl.Length > 0) || item.Selectable)

    {

    writer.Indent--;

    writer.WriteLine();

    writer.WriteEndTag(

    "a");

    }

    }

    else

    {

    writer.Indent--;

    writer.WriteLine();

    writer.WriteEndTag(

    "span");

    }

     

    Despite these changes i find that OnMenuItem Click does not fire ,and postbacks do not happen

    Hope someone can help.Thanks 

     

  • Re: When MenuItem.NavigateUrl is not set the MenuItemClick event isn't fired!

    04-27-2007, 12:06 PM
    • Member
      6 point Member
    • askforprem
    • Member since 04-27-2007, 2:51 PM
    • Posts 3

    Hi all,

      I have been following this thread since i have the exact same problem.Am new to .NET 2.0

    and greatly appreciate any help.

    Here's my situation

    I have a main menu which is horizontal,and am using the CSS Control adapters(menu)

    The Menu picks its items from the Web.Sitemap which is as follows

    <?xml version="1.0" encoding="utf-8" ?>

    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="~/Home.aspx" title="Home" description="Home">

    <siteMapNode title="VDM" url="~/VDM/Default.aspx" description="Basic Reporting Samples">

    </siteMapNode>

    <

    siteMapNode title="DataDashBoard" url="~/DataDashBoard/Default.aspx" description="Contains DashBoard Info">

    </

    siteMapNode>

    <

    siteMapNode title="Logout" url="~/Logout.aspx" description="Logout Page">

    </

    siteMapNode>

    </

    siteMapNode>

    </

    siteMap>

     

    The Menu in the MasterPage.aspx.cs looks like this

     <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal" CssSelectorClass="PrettyMenu" OnMenuItemClick="Menu1_MenuItemClick" Height="28px" Width="518px">

     

     

    </asp:Menu></span>

    <asp

    :SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />

     I modifed the MenuAdapter.cs file in the BuildItem() to look like this

    if

    (IsLink(item))

    {

    //New code from here

     

    //To here

    if ((item.NavigateUrl.Length > 0) || item.Selectable)

    {

    writer.WriteBeginTag(

    "a");

    if (!String.IsNullOrEmpty(item.NavigateUrl))

    {

    writer.WriteAttribute(

    "href", Page.Server.HtmlEncode(Page.ResolveUrl(item.NavigateUrl)));

    //writer.WriteAttribute("href", Page.Server.HtmlEncode(menu.ResolveClientUrl(item.NavigateUrl)));

    }

    }

    else

    {

    writer.WriteAttribute(

    "href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));

    //writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));

    }

    writer.WriteAttribute(

    "class", "AspNet-Menu-Link");

    // writer.WriteAttribute("class", GetItemClass(menu, item));

    //WebControlAdapterExtender.WriteTargetAttribute(writer, item.Target);

     And also the end of the method like this

    if ((item.NavigateUrl.Length > 0) || item.Selectable)

    {

    writer.Indent--;

    writer.WriteLine();

    writer.WriteEndTag(

    "a");

    }

    }

    else

    {

    writer.Indent--;

    writer.WriteLine();

    writer.WriteEndTag(

    "span");

    }

     

    Despite these changes i find that OnMenuItem Click does not fire ,and postbacks do not happen

    Hope someone can help.Thanks 

     

Page 1 of 2 (20 items) 1 2 Next >