Menu control a step behind on Selected Item

Discussion in 'ASP .Net Web Controls' started by Leslie, Mar 28, 2007.

  1. Leslie

    Leslie Guest

    When processing a page load during a postback the menu control never shows
    the menu item that was just clicked when referencing SelectedItem. Instead it
    is always one step behind.

    On the other hand when using the TreeView control and referencing
    SelectedNode the TreeView control is always current.

    Why is the Menu control not current during PageLoad postback?

    I had wanted to use the Menu control in a master page and then reference the
    SelectedItem from a content page. However, when I reference the Menu control
    SelectedItem property from the content page it is always one step behind.

    The source below demonstrates the simple case for PageLoad postback and does
    not use a masterpage content page approach.

    Thanks,

    Leslie



    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Menu1.SelectedItem != null)
    {
    Response.Write("Menu selected item = " + Menu1.SelectedItem.Text);
    }
    else
    {
    Response.Write("Menu selected item = " + "No item selected");
    }
    Response.Write("<P/>");
    if (TreeView1.SelectedNode != null)
    {
    Response.Write("TreeView selected item = " + TreeView1.SelectedNode.Text);
    }
    else
    {
    Response.Write("TreeView selected item = " + "No item selected");
    }

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    Notice the difference between the Selected item for a Menu control and the
    Selected
    Node for a TreeView Control!<br />
    <br />
    The Menu control is always a step behind on the Selected item during
    postback while
    the TreeView is always current on postback.
    <br />
    <br />
    Why would this be?</div>
    <br />
    <asp:Menu ID="Menu1" runat="server">
    <Items>
    <asp:MenuItem Text="MenuItem1" Value="Item1"></asp:MenuItem>
    <asp:MenuItem Text="MenuItem2" Value="Item2"></asp:MenuItem>
    <asp:MenuItem Text="MenuItem3" Value="Item3"></asp:MenuItem>
    </Items>
    </asp:Menu>
    <asp:TreeView ID="TreeView1" runat="server">
    <Nodes>
    <asp:TreeNode Text="Root Node" Value="Root Node">
    <asp:TreeNode Text="Node1" Value="Node1"></asp:TreeNode>
    <asp:TreeNode Text="Node2" Value="Node2"></asp:TreeNode>
    <asp:TreeNode Text="Node3" Value="Node3"></asp:TreeNode>
    </asp:TreeNode>
    </Nodes>
    </asp:TreeView>

    </form>
    </body>
    </html>
    Leslie, Mar 28, 2007
    #1
    1. Advertising

  2. Hi Leslie,

    The difference between the Menu and TreeView here is that TreeView
    implemented an additional interface IPostBackDataHandler and it will get
    the SelectedNode property before Page_Load. The menu's SelectedItem
    property is updated using MenuItem's Click event which is raised after
    Page_Load stage, therefore you're seeing the delay.

    Workaround:

    We can directly inspect the Request.Form to determine if the postback is
    caused by the Menu and get the selected MenuItem:

    protected void Page_Load(object sender, EventArgs e)
    {
    MenuItem mi = GetSelectedMenuItem(Menu1);

    if (mi != null)
    {
    Response.Write("Menu selected item = " + mi.Text);
    }
    else
    {
    Response.Write("Menu selected item = " + "No item selected");
    }
    Response.Write("<P/>");
    if (TreeView1.SelectedNode != null)
    {
    Response.Write("TreeView selected item = " +
    TreeView1.SelectedNode.Text);
    }
    else
    {
    Response.Write("TreeView selected item = " + "No item selected");
    }

    }

    MenuItem GetSelectedMenuItem(Menu menu)
    {
    MenuItem selectedItem = null;
    if (Request.Form["__EVENTTARGET"] == menu.ID)
    {
    string value = Request.Form["__EVENTARGUMENT"];
    if (!string.IsNullOrEmpty(value))
    {
    foreach (MenuItem mi in menu.Items)
    {
    if (mi.Value == value)
    {
    selectedItem = mi;
    break;
    }
    }
    }
    }
    else
    {
    selectedItem = menu.SelectedItem;
    }
    return selectedItem;
    }


    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Mar 28, 2007
    #2
    1. Advertising

  3. Leslie

    Leslie Guest

    Walter,

    Thanks for a good explanation as well as a good workaround.

    Leslie

    "Walter Wang [MSFT]" wrote:

    > Hi Leslie,
    >
    > The difference between the Menu and TreeView here is that TreeView
    > implemented an additional interface IPostBackDataHandler and it will get
    > the SelectedNode property before Page_Load. The menu's SelectedItem
    > property is updated using MenuItem's Click event which is raised after
    > Page_Load stage, therefore you're seeing the delay.
    >
    > Workaround:
    >
    > We can directly inspect the Request.Form to determine if the postback is
    > caused by the Menu and get the selected MenuItem:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > MenuItem mi = GetSelectedMenuItem(Menu1);
    >
    > if (mi != null)
    > {
    > Response.Write("Menu selected item = " + mi.Text);
    > }
    > else
    > {
    > Response.Write("Menu selected item = " + "No item selected");
    > }
    > Response.Write("<P/>");
    > if (TreeView1.SelectedNode != null)
    > {
    > Response.Write("TreeView selected item = " +
    > TreeView1.SelectedNode.Text);
    > }
    > else
    > {
    > Response.Write("TreeView selected item = " + "No item selected");
    > }
    >
    > }
    >
    > MenuItem GetSelectedMenuItem(Menu menu)
    > {
    > MenuItem selectedItem = null;
    > if (Request.Form["__EVENTTARGET"] == menu.ID)
    > {
    > string value = Request.Form["__EVENTARGUMENT"];
    > if (!string.IsNullOrEmpty(value))
    > {
    > foreach (MenuItem mi in menu.Items)
    > {
    > if (mi.Value == value)
    > {
    > selectedItem = mi;
    > break;
    > }
    > }
    > }
    > }
    > else
    > {
    > selectedItem = menu.SelectedItem;
    > }
    > return selectedItem;
    > }
    >
    >
    > Sincerely,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications. If you are using Outlook Express, please make sure you clear the
    > check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    > promptly.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    Leslie, Mar 28, 2007
    #3
  4. Leslie

    Leslie Guest

    Hello,

    I am reviewing code that is based upon the example below. However, when I
    attempt to compile the code, I get a message that references the following
    line:

    selectedItem = menu.SelectedItem;
    The message says:

    'Menu' does not contain a definition for 'SelectedItem'.



    I am using VS 2005 with the following Versioning information from Help->About:
    (Also, I have .NET Framework 3.0 SP1 installed on my system)

    Microsoft Visual Studio 2005
    Version 8.0.50727.762 (SP.050727-7600)
    Microsoft .NET Framework
    Version 2.0.50727 SP1

    Installed Edition: Enterprise

    Microsoft Visual Basic 2005 77642-113-3000004-41253
    Microsoft Visual Basic 2005

    Microsoft Visual C# 2005 77642-113-3000004-41253
    Microsoft Visual C# 2005

    Microsoft Visual C++ 2005 77642-113-3000004-41253
    Microsoft Visual C++ 2005

    Microsoft Visual Studio Tools for Office 77642-113-3000004-41253
    Microsoft Visual Studio Tools for the Microsoft Office System

    Microsoft Visual Web Developer 2005 77642-113-3000004-41253
    Microsoft Visual Web Developer 2005

    Microsoft Web Application Projects 2005 77642-113-3000004-41253
    Microsoft Web Application Projects 2005
    Version 8.0.50727.762

    Microsoft Web Deployment Projects 2005 77642-113-3000004-41253
    Microsoft Web Deployment Projects 2005

    Visual Studio 2005 Team Edition for Developers 77642-113-3000004-41253
    Microsoft Visual Studio 2005 Team Edition for Software Developers

    Crystal Reports AAC60-G0CSA4B-V7000AY
    Crystal Reports for Visual Studio 2005


    Dotfuscator Professional Edition
    Dotfuscator Professional Edition. Copyright (C) 2002-2005 PreEmptive
    Solutions, Inc.

    Microsoft Visual Studio 2005 Tools for the Microsoft Office System - ENU
    Service Pack 1 (KB926601)
    This service pack is for Microsoft Visual Studio 2005 Tools for the
    Microsoft Office System - ENU.
    If you later install a more recent service pack, this service pack will be
    uninstalled automatically.
    For more information, visit http://support.microsoft.com/kb/926601

    Security Update for Microsoft Visual Studio 2005 Team Edition for Software
    Developers - ENU (KB937061)
    This Security Update is for Microsoft Visual Studio 2005 Team Edition for
    Software Developers - ENU.
    If you later install a more recent service pack, this Security Update will
    be uninstalled automatically.
    For more information, visit http://support.microsoft.com/kb/937061

    SQL Server Analysis Services
    Microsoft SQL Server Analysis Services Designer
    Version 9.00.2047.00

    SQL Server Integration Services
    Microsoft SQL Server Integration Services Designer
    Version 9.00.2047.00

    SQL Server Reporting Services
    Microsoft SQL Server Reporting Services Designers
    Version 9.00.2047.00

    Visual Studio Tools for MySQL 1.0
    Data design and management tools for MySQL. Copyright © 2006 MySQL AB


    "Leslie" wrote:

    > Walter,
    >
    > Thanks for a good explanation as well as a good workaround.
    >
    > Leslie
    >
    > "Walter Wang [MSFT]" wrote:
    >
    > > Hi Leslie,
    > >
    > > The difference between the Menu and TreeView here is that TreeView
    > > implemented an additional interface IPostBackDataHandler and it will get
    > > the SelectedNode property before Page_Load. The menu's SelectedItem
    > > property is updated using MenuItem's Click event which is raised after
    > > Page_Load stage, therefore you're seeing the delay.
    > >
    > > Workaround:
    > >
    > > We can directly inspect the Request.Form to determine if the postback is
    > > caused by the Menu and get the selected MenuItem:
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > MenuItem mi = GetSelectedMenuItem(Menu1);
    > >
    > > if (mi != null)
    > > {
    > > Response.Write("Menu selected item = " + mi.Text);
    > > }
    > > else
    > > {
    > > Response.Write("Menu selected item = " + "No item selected");
    > > }
    > > Response.Write("<P/>");
    > > if (TreeView1.SelectedNode != null)
    > > {
    > > Response.Write("TreeView selected item = " +
    > > TreeView1.SelectedNode.Text);
    > > }
    > > else
    > > {
    > > Response.Write("TreeView selected item = " + "No item selected");
    > > }
    > >
    > > }
    > >
    > > MenuItem GetSelectedMenuItem(Menu menu)
    > > {
    > > MenuItem selectedItem = null;
    > > if (Request.Form["__EVENTTARGET"] == menu.ID)
    > > {
    > > string value = Request.Form["__EVENTARGUMENT"];
    > > if (!string.IsNullOrEmpty(value))
    > > {
    > > foreach (MenuItem mi in menu.Items)
    > > {
    > > if (mi.Value == value)
    > > {
    > > selectedItem = mi;
    > > break;
    > > }
    > > }
    > > }
    > > }
    > > else
    > > {
    > > selectedItem = menu.SelectedItem;
    > > }
    > > return selectedItem;
    > > }
    > >
    > >
    > > Sincerely,
    > > Walter Wang (, remove 'online.')
    > > Microsoft Online Community Support
    > >
    > > ==================================================
    > > Get notification to my posts through email? Please refer to
    > > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > > ications. If you are using Outlook Express, please make sure you clear the
    > > check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    > > promptly.
    > >
    > > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > > where an initial response from the community or a Microsoft Support
    > > Engineer within 1 business day is acceptable. Please note that each follow
    > > up response may take approximately 2 business days as the support
    > > professional working with you may need further investigation to reach the
    > > most efficient resolution. The offering is not appropriate for situations
    > > that require urgent, real-time or phone-based interactions or complex
    > > project analysis and dump analysis issues. Issues of this nature are best
    > > handled working with a dedicated Microsoft Support Engineer by contacting
    > > Microsoft Customer Support Services (CSS) at
    > > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > > ==================================================
    > >
    > > This posting is provided "AS IS" with no warranties, and confers no rights.
    > >
    > >
    Leslie, May 12, 2008
    #4
  5. Leslie

    Leslie Guest

    This problem was caused by a naming collision. I had created a form called
    "Menu" which created a "Menu" class. Once I renamed my "Menu" class to
    "Menu1" class everything compiles fine.

    Leslie

    "Leslie" wrote:

    > Hello,
    >
    > I am reviewing code that is based upon the example below. However, when I
    > attempt to compile the code, I get a message that references the following
    > line:
    >
    > selectedItem = menu.SelectedItem;
    > The message says:
    >
    > 'Menu' does not contain a definition for 'SelectedItem'.
    >
    >
    >
    > I am using VS 2005 with the following Versioning information from Help->About:
    > (Also, I have .NET Framework 3.0 SP1 installed on my system)
    >
    > Microsoft Visual Studio 2005
    > Version 8.0.50727.762 (SP.050727-7600)
    > Microsoft .NET Framework
    > Version 2.0.50727 SP1
    >
    > Installed Edition: Enterprise
    >
    > Microsoft Visual Basic 2005 77642-113-3000004-41253
    > Microsoft Visual Basic 2005
    >
    > Microsoft Visual C# 2005 77642-113-3000004-41253
    > Microsoft Visual C# 2005
    >
    > Microsoft Visual C++ 2005 77642-113-3000004-41253
    > Microsoft Visual C++ 2005
    >
    > Microsoft Visual Studio Tools for Office 77642-113-3000004-41253
    > Microsoft Visual Studio Tools for the Microsoft Office System
    >
    > Microsoft Visual Web Developer 2005 77642-113-3000004-41253
    > Microsoft Visual Web Developer 2005
    >
    > Microsoft Web Application Projects 2005 77642-113-3000004-41253
    > Microsoft Web Application Projects 2005
    > Version 8.0.50727.762
    >
    > Microsoft Web Deployment Projects 2005 77642-113-3000004-41253
    > Microsoft Web Deployment Projects 2005
    >
    > Visual Studio 2005 Team Edition for Developers 77642-113-3000004-41253
    > Microsoft Visual Studio 2005 Team Edition for Software Developers
    >
    > Crystal Reports AAC60-G0CSA4B-V7000AY
    > Crystal Reports for Visual Studio 2005
    >
    >
    > Dotfuscator Professional Edition
    > Dotfuscator Professional Edition. Copyright (C) 2002-2005 PreEmptive
    > Solutions, Inc.
    >
    > Microsoft Visual Studio 2005 Tools for the Microsoft Office System - ENU
    > Service Pack 1 (KB926601)
    > This service pack is for Microsoft Visual Studio 2005 Tools for the
    > Microsoft Office System - ENU.
    > If you later install a more recent service pack, this service pack will be
    > uninstalled automatically.
    > For more information, visit http://support.microsoft.com/kb/926601
    >
    > Security Update for Microsoft Visual Studio 2005 Team Edition for Software
    > Developers - ENU (KB937061)
    > This Security Update is for Microsoft Visual Studio 2005 Team Edition for
    > Software Developers - ENU.
    > If you later install a more recent service pack, this Security Update will
    > be uninstalled automatically.
    > For more information, visit http://support.microsoft.com/kb/937061
    >
    > SQL Server Analysis Services
    > Microsoft SQL Server Analysis Services Designer
    > Version 9.00.2047.00
    >
    > SQL Server Integration Services
    > Microsoft SQL Server Integration Services Designer
    > Version 9.00.2047.00
    >
    > SQL Server Reporting Services
    > Microsoft SQL Server Reporting Services Designers
    > Version 9.00.2047.00
    >
    > Visual Studio Tools for MySQL 1.0
    > Data design and management tools for MySQL. Copyright © 2006 MySQL AB
    >
    >
    > "Leslie" wrote:
    >
    > > Walter,
    > >
    > > Thanks for a good explanation as well as a good workaround.
    > >
    > > Leslie
    > >
    > > "Walter Wang [MSFT]" wrote:
    > >
    > > > Hi Leslie,
    > > >
    > > > The difference between the Menu and TreeView here is that TreeView
    > > > implemented an additional interface IPostBackDataHandler and it will get
    > > > the SelectedNode property before Page_Load. The menu's SelectedItem
    > > > property is updated using MenuItem's Click event which is raised after
    > > > Page_Load stage, therefore you're seeing the delay.
    > > >
    > > > Workaround:
    > > >
    > > > We can directly inspect the Request.Form to determine if the postback is
    > > > caused by the Menu and get the selected MenuItem:
    > > >
    > > > protected void Page_Load(object sender, EventArgs e)
    > > > {
    > > > MenuItem mi = GetSelectedMenuItem(Menu1);
    > > >
    > > > if (mi != null)
    > > > {
    > > > Response.Write("Menu selected item = " + mi.Text);
    > > > }
    > > > else
    > > > {
    > > > Response.Write("Menu selected item = " + "No item selected");
    > > > }
    > > > Response.Write("<P/>");
    > > > if (TreeView1.SelectedNode != null)
    > > > {
    > > > Response.Write("TreeView selected item = " +
    > > > TreeView1.SelectedNode.Text);
    > > > }
    > > > else
    > > > {
    > > > Response.Write("TreeView selected item = " + "No item selected");
    > > > }
    > > >
    > > > }
    > > >
    > > > MenuItem GetSelectedMenuItem(Menu menu)
    > > > {
    > > > MenuItem selectedItem = null;
    > > > if (Request.Form["__EVENTTARGET"] == menu.ID)
    > > > {
    > > > string value = Request.Form["__EVENTARGUMENT"];
    > > > if (!string.IsNullOrEmpty(value))
    > > > {
    > > > foreach (MenuItem mi in menu.Items)
    > > > {
    > > > if (mi.Value == value)
    > > > {
    > > > selectedItem = mi;
    > > > break;
    > > > }
    > > > }
    > > > }
    > > > }
    > > > else
    > > > {
    > > > selectedItem = menu.SelectedItem;
    > > > }
    > > > return selectedItem;
    > > > }
    > > >
    > > >
    > > > Sincerely,
    > > > Walter Wang (, remove 'online.')
    > > > Microsoft Online Community Support
    > > >
    > > > ==================================================
    > > > Get notification to my posts through email? Please refer to
    > > > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > > > ications. If you are using Outlook Express, please make sure you clear the
    > > > check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    > > > promptly.
    > > >
    > > > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > > > where an initial response from the community or a Microsoft Support
    > > > Engineer within 1 business day is acceptable. Please note that each follow
    > > > up response may take approximately 2 business days as the support
    > > > professional working with you may need further investigation to reach the
    > > > most efficient resolution. The offering is not appropriate for situations
    > > > that require urgent, real-time or phone-based interactions or complex
    > > > project analysis and dump analysis issues. Issues of this nature are best
    > > > handled working with a dedicated Microsoft Support Engineer by contacting
    > > > Microsoft Customer Support Services (CSS) at
    > > > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > > > ==================================================
    > > >
    > > > This posting is provided "AS IS" with no warranties, and confers no rights.
    > > >
    > > >
    Leslie, May 12, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jaap de verwant slachter
    Replies:
    0
    Views:
    1,257
    jaap de verwant slachter
    Jul 1, 2003
  2. Roy in

    need step by step example

    Roy in, Aug 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    354
    Roy in
    Aug 3, 2003
  3. Steve Richter

    a step by step page

    Steve Richter, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    375
    Steve Richter
    May 3, 2005
  4. Replies:
    0
    Views:
    1,429
  5. Li Pang

    Step by step to create/consum a user control

    Li Pang, Sep 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    126
    Brock Allen
    Sep 5, 2005
Loading...

Share This Page