0
votes

I've created an application in which I've created Dynamic Controls on Click Event of a Button. But the dynamically created Control Event will not Fire on Click of it. Below is the Code I've written.

   ImageButton closeImage = new ImageButton();
        closeImage.ID = "ID"+ dr["ID"].ToString();
        closeImage.AlternateText = "Delete";
        closeImage.ImageUrl = "App_Themes/images/CloseButton.png";
        closeImage.Attributes.Add("style", "float:right;cursor:pointer;margin: -19px 1px -10px;");
        closeImage.EnableViewState = true;
        closeImage.CommandName = "Delete";
        closeImage.CommandArgument = dr["ID"].ToString();
        closeImage.Attributes.Add("runat", "server");
        closeImage.OnClientClick = "return confirm('Are you sure you want to remove this');";
        closeImage.Click += new ImageClickEventHandler(this.closeImage_click);


  protected void closeImage_click(object sender, ImageClickEventArgs e)
{
    ImageButton ibDel = (ImageButton)sender;
}

The same code work if its placed on Page_Load(). Is there any other Solution to fire the event.

1

1 Answers

1
votes

I would like to suggest that you read up on the following article on MSDN. It's about page lifecycle.The key part is to when controls are created. If the controls are created in the wrong stage, events will not be registered, and thus will not fire.

http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.80%29.aspx