Why dynamically created Radmenu ItemClick event is not firing

1.3k Views Asked by At

I am dynamically creating a RadMenu. I want to use the menu click event of the RadMenu.

Actually I am using if(!Scriptmanager.IsInAsyncPostBack) condition.

Within the if condition I only write code to create the menu dynamically. I'll give the sample code for better understanding.

if (!src.IsInAsyncPostBack)
{                  
    RadMenu menu = new RadMenu();
    RadMenuItem item1 = new RadMenuItem();
    item1.Text = "Home";
    RadMenuItem item11 = new RadMenuItem();
    item11.Text = "Home";
    item1.Items.Add(item11);                 
    RadMenuItem item2 = new RadMenuItem();
    item2.Text = "About";
    RadMenuItem item3 = new RadMenuItem();
    item3.Text = "Contact";
    menu.ItemClick += new RadMenuEventHandler(menu_ItemClick);                  

    menu.Items.Add(item1);
    menu.Items.Add(item2);
    menu.Items.Add(item3);
    Page.Controls.Add(menu);
}

void menu_ItemClick(object sender, RadMenuEventArgs e)
{
    Response.Redirect("Home.aspx");
}

When page loading if(!IsInAsyncPostBack) condition true so the RadMenu is created dynamically and loaded in the page.

When I click the menu item, Postback becomes true, so the if condition fails and the menu click event is not firing.

I wrote the menu click event outside of the if condition. In this case the menu click event is also not firing.

My exact requirement is that I want to use if(!IsInAsyncPostBack) postback condition as well as I need to use menu click event. Is it possible?

1

There are 1 best solutions below

0
On

Add this in the Page_Init event and remove the if (!src.IsInAsyncPostBack) check. THis is the easiest way to create controls programmatically.

Probably your menu is ajax-enabled so a POST from it is partial, so your code does not recreate it, so it cannot raise its handler.