Menu control a step behind on Selected Item

L

Leslie

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>
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
L

Leslie

Walter,

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

Leslie

Walter Wang said:
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 ([email protected], 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.
 
L

Leslie

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 said:
Walter,

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

Leslie

Walter Wang said:
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 ([email protected], 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.
 
L

Leslie

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 said:
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 said:
Walter,

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

Leslie

Walter Wang said:
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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top