Menu control issue....

J

John Smith

I am trying to use the menu control for a tabbed menu system but it's
frustrating the hell out of me. The full code is listed below. The trouble
that I'm having is that, when the user clicks on a menu item, the page will
send back the id of the menu that was clicked. But it doesn't do that and I
can't figure out how to make it work. Looking at the code below, the
variable "strDBView" is always null. But it's a global variable and gets
assigned a value upon the menu click. I have tried putting that
Response.Write line in the !IsPostBack clause with the same results. If I
place the Response.Write line within the menu click event handler, it does
writes out a value as expected. So, it's like the value gets set and then,
upon page load, wiped out.

Any ideas? I have spent 4 days trying to figure this out.

Thanks in advance.

code follows:
<%@ 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">
string strDBView;

protected void Page_Load(object sender, EventArgs e)
{
string strMenuLabel;

// why is strDBView always null? I'm assigning it a value within
the menu control click evet
Response.Write("<br />Here: " + strDBView + "</br >");

if (!this.IsPostBack)
{
for (int index = 0; index < mvTicketMan.Views.Count; index++)
{
strMenuLabel =
mvTicketMan.Views[index].ID.ToString().Replace("vw", "").Replace("_", " ");
mnuTicketMan.Items.Add(new MenuItem(strMenuLabel,
index.ToString()));
}
mnuTicketMan.Items[0].Selected = true;
}
}

protected void mnuTicketMan_MenuItemClick(object sender, MenuEventArgs
e)
{
mvTicketMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
strDBView =
mvTicketMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
"").Replace("_", " ");
// if I show the value here, it does indeed get assigned a proper
value
//Response.Write("<br />Here: " + strDBView + "</br >");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Menu ID="mnuTicketMan" Width="100%" runat="server"
Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False"
OnMenuItemClick="mnuTicketMan_MenuItemClick">

<StaticMenuItemStyle CssClass=MenuCell ItemSpacing=0px />
<StaticHoverStyle CssClass=MenuCellHover />
<StaticSelectedStyle CssClass=MenuCellSelected ItemSpacing=0px />
</asp:Menu>

<asp:MultiView ID="mvTicketMan" runat="server" ActiveViewIndex="0">

<%--My Tickets--%>
<asp:View ID="vwMy_Tickets" runat="server" >
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End My Tickets--%>

<%--Watched Tickets--%>
<asp:View ID="vwWatched_Tickets" runat="server">
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End Watched Tickets--%>

<asp:View ID="vwAssigned_Tickets" runat="server">
<table width="100%" height="400px" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>

</asp:MultiView>
</div>
</form>
</body>
</html>
 
K

Ken Cox [Microsoft MVP]

Hey John,

I think that you're forgetting that if you want to maintain a variable
between postbacks, you've got to handle it yourself. That is, string
strDBView; only exists for the life of the page. Unless its value is somehow
posted back, the server has no "knowledge" as to what it held previously.

The ASP.NET controls maintain state for themselves but for your global
variable, you need something like:

ViewState["strDVView"] = strDBView;
strDBView = (string)ViewState["strDVView"];

http://msdn.microsoft.com/library/d...rlrfsystemwebuicontrolclassviewstatetopic.asp

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


John Smith said:
I am trying to use the menu control for a tabbed menu system but it's
frustrating the hell out of me. The full code is listed below. The
trouble that I'm having is that, when the user clicks on a menu item, the
page will send back the id of the menu that was clicked. But it doesn't do
that and I can't figure out how to make it work. Looking at the code
below, the variable "strDBView" is always null. But it's a global variable
and gets assigned a value upon the menu click. I have tried putting that
Response.Write line in the !IsPostBack clause with the same results. If I
place the Response.Write line within the menu click event handler, it does
writes out a value as expected. So, it's like the value gets set and then,
upon page load, wiped out.

Any ideas? I have spent 4 days trying to figure this out.

Thanks in advance.

code follows:
<%@ 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">
string strDBView;

protected void Page_Load(object sender, EventArgs e)
{
string strMenuLabel;

// why is strDBView always null? I'm assigning it a value within
the menu control click evet
Response.Write("<br />Here: " + strDBView + "</br >");

if (!this.IsPostBack)
{
for (int index = 0; index < mvTicketMan.Views.Count; index++)
{
strMenuLabel =
mvTicketMan.Views[index].ID.ToString().Replace("vw", "").Replace("_", "
");
mnuTicketMan.Items.Add(new MenuItem(strMenuLabel,
index.ToString()));
}
mnuTicketMan.Items[0].Selected = true;
}
}

protected void mnuTicketMan_MenuItemClick(object sender, MenuEventArgs
e)
{
mvTicketMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
strDBView =
mvTicketMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
"").Replace("_", " ");
// if I show the value here, it does indeed get assigned a proper
value
//Response.Write("<br />Here: " + strDBView + "</br >");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Menu ID="mnuTicketMan" Width="100%" runat="server"
Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False"
OnMenuItemClick="mnuTicketMan_MenuItemClick">

<StaticMenuItemStyle CssClass=MenuCell ItemSpacing=0px />
<StaticHoverStyle CssClass=MenuCellHover />
<StaticSelectedStyle CssClass=MenuCellSelected ItemSpacing=0px />
</asp:Menu>

<asp:MultiView ID="mvTicketMan" runat="server" ActiveViewIndex="0">

<%--My Tickets--%>
<asp:View ID="vwMy_Tickets" runat="server" >
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End My Tickets--%>

<%--Watched Tickets--%>
<asp:View ID="vwWatched_Tickets" runat="server">
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End Watched Tickets--%>

<asp:View ID="vwAssigned_Tickets" runat="server">
<table width="100%" height="400px" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>

</asp:MultiView>
</div>
</form>
</body>
</html>
 
J

John Smith

Ken, you are awesome!! I was overlooking the obvious. That did indeed
solve my problem.

Thank you!
-J



Ken Cox said:
Hey John,

I think that you're forgetting that if you want to maintain a variable
between postbacks, you've got to handle it yourself. That is, string
strDBView; only exists for the life of the page. Unless its value is somehow
posted back, the server has no "knowledge" as to what it held previously.

The ASP.NET controls maintain state for themselves but for your global
variable, you need something like:

ViewState["strDVView"] = strDBView;
strDBView = (string)ViewState["strDVView"];

http://msdn.microsoft.com/library/d...rlrfsystemwebuicontrolclassviewstatetopic.asp

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


John Smith said:
I am trying to use the menu control for a tabbed menu system but it's
frustrating the hell out of me. The full code is listed below. The
trouble that I'm having is that, when the user clicks on a menu item, the
page will send back the id of the menu that was clicked. But it doesn't do
that and I can't figure out how to make it work. Looking at the code
below, the variable "strDBView" is always null. But it's a global variable
and gets assigned a value upon the menu click. I have tried putting that
Response.Write line in the !IsPostBack clause with the same results. If I
place the Response.Write line within the menu click event handler, it does
writes out a value as expected. So, it's like the value gets set and then,
upon page load, wiped out.

Any ideas? I have spent 4 days trying to figure this out.

Thanks in advance.

code follows:
<%@ 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">
string strDBView;

protected void Page_Load(object sender, EventArgs e)
{
string strMenuLabel;

// why is strDBView always null? I'm assigning it a value within
the menu control click evet
Response.Write("<br />Here: " + strDBView + "</br >");

if (!this.IsPostBack)
{
for (int index = 0; index < mvTicketMan.Views.Count; index++)
{
strMenuLabel =
mvTicketMan.Views[index].ID.ToString().Replace("vw", "").Replace("_", "
");
mnuTicketMan.Items.Add(new MenuItem(strMenuLabel,
index.ToString()));
}
mnuTicketMan.Items[0].Selected = true;
}
}

protected void mnuTicketMan_MenuItemClick(object sender, MenuEventArgs
e)
{
mvTicketMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
strDBView =
mvTicketMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
"").Replace("_", " ");
// if I show the value here, it does indeed get assigned a proper
value
//Response.Write("<br />Here: " + strDBView + "</br >");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Menu ID="mnuTicketMan" Width="100%" runat="server"
Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False"
OnMenuItemClick="mnuTicketMan_MenuItemClick">

<StaticMenuItemStyle CssClass=MenuCell ItemSpacing=0px />
<StaticHoverStyle CssClass=MenuCellHover />
<StaticSelectedStyle CssClass=MenuCellSelected ItemSpacing=0px />
</asp:Menu>

<asp:MultiView ID="mvTicketMan" runat="server" ActiveViewIndex="0">

<%--My Tickets--%>
<asp:View ID="vwMy_Tickets" runat="server" >
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End My Tickets--%>

<%--Watched Tickets--%>
<asp:View ID="vwWatched_Tickets" runat="server">
<table width="100%" height="100%" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>
<%--End Watched Tickets--%>

<asp:View ID="vwAssigned_Tickets" runat="server">
<table width="100%" height="400px" cellpadding=0 cellspacing=0>
<tr>
<td class="Canvas">
</td>
</tr>
</table>
</asp:View>

</asp:MultiView>
</div>
</form>
</body>
</html>
 

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

Top