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