Unable to fire Imagebutton_Click event

Posted on 2007-10-16
Last Modified: 2008-01-09
I am creating a datagrid dynamically and one of its columns needs to be a template column. I am able to build the datagrid and also the template column successfully. The template column actually contains an image button and when this button is clicked, some of the datagrid cells should get saved to the database. My problem is that I have defined everything in the code but am unable to fire the imagebutton_click event. Control goes to the event handler but it does not go to the event.

Please let me know if I am missing anything. I am posting the code below:

1. ASPX.CS Code
private void Page_Load(object sender, System.EventArgs e)
                        imgbtn = new ImageButton();
                        imgbtn.Click +=new ImageClickEventHandler(imgbtn_Click);      
2. Class file code where I am creating the template column

public void AddTemplateColumns(string HeaderTexts, string DataTexts, string ImageUrls)
                  string[] HeaderText = HeaderTexts.Split(',');
                  string[] DataText = DataTexts.Split(',');
                  string[] ImageURL = ImageUrls.Split(',');

                  for(int i = 0;i < HeaderText.Length; i++)
                        TemplateColumn TempCol = new TemplateColumn();
                  TempCol.ItemTemplate = new CreateItemTemplateImageButton(HeaderText[i],DataText[i],ImageURL[i]);

3. Implementing the ITemplate Interface:
public class CreateItemTemplateImageButton : ITemplate
            string strImageURL;
            string strImageButtonName;
            string strCommandName;
            bool Visibility = true;
            public CreateItemTemplateImageButton(string ImageButtonName,string ImageUrl,string CommandName)
                  this.strImageURL =ImageUrl;
            public CreateItemTemplateImageButton(string ImageButtonName,string ImageUrl,string CommandName,bool Visibility)
                  this.strImageURL =ImageUrl;
            public void InstantiateIn(Control objContainer)
                  ImageButton imgbtn = new ImageButton();
                  imgbtn.DataBinding+=new EventHandler(imgbtn_DataBinding);
                  imgbtn.Click +=new ImageClickEventHandler(imgbtn_Click);
            private void imgbtn_DataBinding(object sender, EventArgs e)
                  ImageButton imgBtn = (ImageButton)sender;
This is my Itemdatabound event code:
private void dgTest_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
                  imgbtn = ((ImageButton)e.Item.Cells[3].FindControl("imgBtnID"));
Question by:haritha_204
    LVL 3

    Accepted Solution

    Where's imgbtn_click?  Have you tried "Trace.IsEnabled = True" and putting a "Trace.Write("imgbtn_Click fired")" in the event to validate it's being called?

    Author Comment

    imgbtn_click is in the aspx.cs file.. I did try putting Trace.IsEnabled='True'. Anyway I found an alternative to serve my purpose.
    But I would definitely like to know how my problem could be solved.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now