Menu control issue....

Discussion in 'ASP .Net' started by John Smith, Aug 3, 2006.

  1. John Smith

    John Smith Guest

    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>
     
    John Smith, Aug 3, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    >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>
    >
    >
    >
    >
     
    Ken Cox [Microsoft MVP], Aug 4, 2006
    #2
    1. Advertising

  3. John Smith

    John Smith Guest

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

    Thank you!
    -J



    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > >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>
    > >
    > >
    > >
    > >

    >
    >
     
    John Smith, Aug 4, 2006
    #3
    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. Bryan
    Replies:
    1
    Views:
    796
    Steven Cheng[MSFT]
    Mar 1, 2006
  2. Replies:
    0
    Views:
    1,478
  3. empire5
    Replies:
    1
    Views:
    590
    =?Utf-8?B?UmVuZ2E=?=
    Jan 11, 2007
  4. Mike Tavares

    Adding Access keys to menu items in the Menu control

    Mike Tavares, Dec 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    426
  5. Ivann
    Replies:
    1
    Views:
    208
    Ivann
    Jul 22, 2008
Loading...

Share This Page