Server-Side Event Binding on Dynamic Menu

5 posts, 1 answers
  1. Veenu
    Veenu avatar
    45 posts
    Member since:
    Jun 2008

    Posted 14 Oct 2009 Link to this post

    Hi,

    I've a RadToolbar placed in the Master Page which contains the logic to draw Dynamic RadMenus depending upon the page.
    e.g. I need Download Menu on some application pages, and Filters Menu on some other page, but no Menu on most of the pages.
    So, in the Master Page, there is a logic to check the page URL and create a RadMenu and its required SubMenus.
    All this logic is outside the Page.IsPostBack, as I need to redraw the same Menus if there is a page-postback.

    Now I try to bind a server-side event on the RadMenu Item Click. Event-binding is done properly and code compiles.
    But when the Item is clicked, there is a postback and the Menu is redarwn but the event is not fired.
    Can you please direct me why? This is really urgent as this is the central logic to be used for pages and is not working :(

    I'd like to mention here that the ToolBar and Dynamic Menu logic was wrapped inside an UpdatePanel to have the possibility to fire a server-side event to reload only the ToolBar and the Dynamic Menu without having a complete page refresh.
    So is there an issue with the approach, the implementation or the RadMenu?
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 15 Oct 2009 Link to this post

    Hello Veenu,

    When do you execute the code which creates the menu? Could you paste it here?

    You should subscribe to the ItemClick event as early as possible in the page lifecycle (preferably during Page_Load).

    Regards,
    Albert,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Veenu
    Veenu avatar
    45 posts
    Member since:
    Jun 2008

    Posted 13 Nov 2009 Link to this post

    Hi Albert,
    I've attached snapshots of code. Here is a description of the approach:
    On the MasterPage, we've placed a static RadToolbar at design-time which is to act as the container for Dynamic Menus (code below)

    <

     

    telerik:RadToolBar ID="rtbMenuBar" runat="server" EnableEmbeddedSkins="true"

     

     

    Orientation="Horizontal" >

     

     

    <Items>

     

     

    </Items>

     

     

    </telerik:RadToolBar>

     

    and additionally, a static Menu has been placed in Design (code below)

    <

     

    telerik:RadMenu ID="rdStaticMenu" runat="server" OnItemClick="RadMenuStatic_ItemClicked">

     

     

    <Items>

     

     

    <telerik:RadMenuItem id="ID1" runat="server" Text="Child1"></telerik:RadMenuItem>

     

     

    <telerik:RadMenuItem id="ID2" runat="server" Text="Child2"></telerik:RadMenuItem>

     

     

    <telerik:RadMenuItem id="ID3" runat="server" Text="Child3"></telerik:RadMenuItem>

     

     

    </Items>

     

     

    </telerik:RadMenu>

    Now, in the Code-behind for the Master page, you'll find in the Page_Load() method, you'll find that a RadMenu and few SubMenus are created in the RadToolBar, and all Menus are binded to a server-side event (Code below)

     

    //Code to build Dynamic Menu

     

     

     

     

    RadMenu objRadMenu = new RadMenu();

     

    objRadMenu.ID =

    "objMenu";

     

    objRadMenu.DefaultGroupSettings.Height =

    Unit.Pixel(150);

     

    objRadMenu.OnClientItemClicked =

    "MenuItemClicked";

     

    objRadMenu.ItemClick +=

    new RadMenuEventHandler(RadMenu_ItemClicked);

     

    objRadMenu.EnableEmbeddedSkins =

    true;

     

     

     

    RadMenuItem radItem1 = new RadMenuItem("Item1");

     

     

    RadMenuItem radItem2 = new RadMenuItem("Item2");

     

     

    RadMenuItem radItem3 = new RadMenuItem("Item3");

     

    objRadMenu.Items.Add(radItem1);

    objRadMenu.Items.Add(radItem2);

    objRadMenu.Items.Add(radItem3);

     

    RadToolBarButton rdTBButton = new RadToolBarButton();

     

    rdTBButton.AllowSelfUnCheck =

    true;

     

    rdTBButton.CheckOnClick =

    true;

     

    rdTBButton.Controls.Add(objRadMenu);

    rtbMenuBar.Items.Add(rdTBButton);

    Code contains the EventHandlers - RadMenuStatic_ItemClicked & RadMenu_ItemClicked. Clicking on the Static Menu results on event being fired, but clicking on the dynamic menu items created above fires nothing.

    //Event for Dynamic Menu
        protected void RadMenu_ItemClicked(object sender, RadMenuEventArgs e)
        {
            lblMessage.Text = "Item Clicked " + e.Item.Text;
        }

        //Event for Static Menu
        protected void RadMenuStatic_ItemClicked(object sender, RadMenuEventArgs e)
        {
            lblMessage.Text = "Item Clicked " + e.Item.Text;
        }

        //Event for Dynamic Button
        protected void btn1_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Item Clicked ";// +e.Item.Text;
        }

    We checked the dynamic behaviour be placing a standard asp:button dynamically in Page Load and attach it to an event on runtime, and it fires. So this is only in case of Telerik menus that Events binded to Menus created at runtime doens't work.
    Please check and help me out. Thanks!

  4. Answer
    Peter
    Admin
    Peter avatar
    6637 posts

    Posted 19 Nov 2009 Link to this post

    Hello Veenu,

    Please, try moving all the code you have in Page Load to Page Init. This should fix the problem, but please feel free to get back to us if it persists.


    Kind regards,
    Peter
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Veenu
    Veenu avatar
    45 posts
    Member since:
    Jun 2008

    Posted 19 Nov 2009 Link to this post

    Hi Peter,

    It worked!!! Thanks a lot for your time and effort. :)

    Best Regards,
    Veenu Munjal
Back to Top