locked
Disable caching ASP.NET Menu items RRS feed

  • Question

  • User2134985357 posted

    Hi,

    i am using a WebSiteMap as a data source for my ASP.NET Menu control. To do the data binding, i am not using any XMLDataSource or SiteMapDataSource. This is because i have complex rules as to which menu item i can display and which i cannot. So i loop through each node of the websitemap and then add new items to the ASP.NET Menu control.

    The problem i am having is that even if i add an item to the ASP.NET Menu, the item does not get displayed. But if i Refresh the page (Press F5), the menu item gets displayed. i also read that if we use the XMLDataSource we need to set the EnableCaching to FALSE to disable caching. Here since i am not using that data source, i dont know how to disable caching in the ASP.NET menu.

    Please help.

    Ullas

    Tuesday, April 19, 2011 10:12 PM

Answers

  • User-578657687 posted

    Ok, I have modified my code based on your information. Please check the codes below:

    Default.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="64px">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
    </asp:Content>

    Default.aspx.cs:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Menu m = (Menu)Master.FindControl("MM1").FindControl("Menu1");
            m.Items.Clear();
            string value = DropDownList1.SelectedValue;
            MenuItem mi = new MenuItem("I am " + value);
            m.Items.Add(mi);
        }
    }

    MasterPage.master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
    <%@ Register TagPrefix="MMM" TagName="MM" Src="~/WebUserControl.ascx" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder id="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
            
            </asp:ContentPlaceHolder>
        <MMM:MM ID="MM1" runat="server" />
    
        </div>
        </form>
    </body>
    </html>
    

    MasterPage.master.cs:

    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }

    WebUserControl.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Menu ID="Menu1" runat="server" onprerender="Menu1_PreRender">
    </asp:Menu>
    

    WebUserControl.ascx.cs:

    public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Menu1_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MenuItem mi = new MenuItem("I am 1");
                Menu1.Items.Add(mi);
            }
        }
       
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2011 5:21 AM

All replies

  • User877341716 posted

    Are you trying to add items to the Menu on Page Load or some click event ?

    Wednesday, April 20, 2011 4:09 AM
  • User-59962394 posted

    To diable caching from code behind, you can use the below

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    

    Hope this helps.

    Thanks.

    Wednesday, April 20, 2011 5:01 AM
  • User2134985357 posted

    The Menu is part of a User control which is registered in the Master page. And i am adding items to the Menu in the control's Page Pre Render event.

    Wednesday, April 20, 2011 10:12 PM
  • User2134985357 posted

    Hi Swapna, i tried disabling caching with that code but it does not work.

    Sunday, April 24, 2011 8:25 PM
  • User-59962394 posted

    Hi

    I am not sure why its not working for you to disable caching as I used it in my project and works fine and also see the below problem where it is used to disable caching.

    http://forums.asp.net/t/1672715.aspx/1?Problem+with+logout

    May be it should be different for you menucontrol scenario.

    Thanks.

    Wednesday, April 27, 2011 4:46 AM
  • User-578657687 posted

    What do you mean for control’s Page PreRender  event? I guess that you want to loop through each node of WebSiteMap and then add relevant nodes as new items into the Menu control. If you agree with me, please check the sample:

    1. Create UserControl and add Menu control
      <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
      <asp:Menu ID="Menu1" runat="server">
      </asp:Menu>
      

         2. Add PreRender event into Menu control

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
           <asp:Menu ID="Menu1" runat="server"  onprerender="Menu1_PreRender">
    </asp:Menu>
    

          3.  Insert some code into PreRender event

       protected void Menu1_PreRender(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                MenuItem mi = new MenuItem("I am" + i);
                Menu1.Items.Add(mi);
            }
        }
    

          4.Register this UserControl to Default.aspx(also you can use MasterPage.master instead of Default.aspx)

    <%@ Register TagPrefix="MMM" TagName="MM" Src="~/WebUserControl.ascx" %>

      

     

    Wednesday, April 27, 2011 5:16 AM
  • User2134985357 posted

    Hi Mamda,

    Thanks for your reply. I am doing exactly the same. Here is my scenario.

    In the Menu Pre-render event, i am looping through the WebSiteMap and adding the nodes to the Menu control. But i am not adding every item from the WebSiteMap to the Menu items collection. I also have a Dropdown control in the Page. Based on what is selected in the Drop down box, i am selectively adding items from the WebSiteMap to the Menu items collection.

    So for the first time when the page loads, it takes the default value of the Drop down and add the items to Menu collection accordingly. For example the WebSiteMap has 3 nodes and the drop down has 3 values A, B, C. If the drop down value is A i add the first item of the web site map and if drop down value is B i add the 2nd item of the web site map.

    Here when the page loads for the first time, only the first item gets added. Now if i change the drop down item to 2, i expect the 2nd item only to be added to the menu collection but this does not happen. if i go through Debug mode, it appears that the menu is up to date but it does not render properly. But if i press F5, the menu gets updated correctly.

    This is the problem i am facing.

    Wednesday, April 27, 2011 6:24 PM
  • User-578657687 posted

    Hi ullasprabhu,

    There are two points need to be noted:  Firstly,  there’s a difference between page postback and page refresh. Postback occurs when you select one item from DropDownList control(you need to set the true value for this control’s property AutoPostback), at the moment, the Menu items will  continue to add one or multiple items based on its original items.  Secondly, as you described, you need to clear the Menu’s items when you’re triggering the DropDownList’s postback.

    Some codes need to be modified like below:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="88px">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
    <asp:Menu ID="Menu1" runat="server" onprerender="Menu1_PreRender">
    </asp:Menu>
    
    protected void Menu1_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string value = DropDownList1.SelectedValue;
                MenuItem mi = new MenuItem("I am" + value);
                Menu1.Items.Add(mi);
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Menu1.Items.Clear();
            string value = DropDownList1.SelectedValue;
            MenuItem mi = new MenuItem("I am " + value);
            Menu1.Items.Add(mi);
        }
    
    Thursday, April 28, 2011 1:01 AM
  • User2134985357 posted

    Yes i understand that now. But the problem is that the User control that hosts this ASP Menu is included in the Master Page. And the drop down is in a child ASPX page which derives from the Master page. So i am not sure how to clear the Menu items collection and add items to it. I need to somehow raise an event from the ASPX page and handle that event in the user control (which hosts the the ASP Menu) and make the appropriate changes. Any ideas?

    Thursday, April 28, 2011 2:01 AM
  • User-578657687 posted

    Ok, I have modified my code based on your information. Please check the codes below:

    Default.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="64px">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
    </asp:Content>

    Default.aspx.cs:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Menu m = (Menu)Master.FindControl("MM1").FindControl("Menu1");
            m.Items.Clear();
            string value = DropDownList1.SelectedValue;
            MenuItem mi = new MenuItem("I am " + value);
            m.Items.Add(mi);
        }
    }

    MasterPage.master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
    <%@ Register TagPrefix="MMM" TagName="MM" Src="~/WebUserControl.ascx" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <asp:ContentPlaceHolder id="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
            
            </asp:ContentPlaceHolder>
        <MMM:MM ID="MM1" runat="server" />
    
        </div>
        </form>
    </body>
    </html>
    

    MasterPage.master.cs:

    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }

    WebUserControl.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Menu ID="Menu1" runat="server" onprerender="Menu1_PreRender">
    </asp:Menu>
    

    WebUserControl.ascx.cs:

    public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Menu1_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MenuItem mi = new MenuItem("I am 1");
                Menu1.Items.Add(mi);
            }
        }
       
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2011 5:21 AM
  • User2134985357 posted

    Thanks Mamba. I did all that and that still did not work. I later realized that my drop down in the user control is Ajax-ified and the drop down does not update the Header control. I have added this update and it works fine now.

    Thanks for your time in helping me through this. You rock!!

     

    Ullas

     

    Friday, April 29, 2011 12:19 AM