• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1095
  • Last Modified:

Unable to fire Imagebutton_Click event

Hi,
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.EnableViewState=true;
                        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]);
                  this.dg.Columns.Add(TempCol);
                  }
                  
            }

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;
                  this.strImageButtonName=ImageButtonName;
                  this.strCommandName=CommandName;
            }
            public CreateItemTemplateImageButton(string ImageButtonName,string ImageUrl,string CommandName,bool Visibility)
            {
                  this.strImageURL =ImageUrl;
                  this.strImageButtonName=ImageButtonName;
                  this.Visibility=Visibility;
                  this.strCommandName=CommandName;
            }
            public void InstantiateIn(Control objContainer)
            {
                  ImageButton imgbtn = new ImageButton();
                  imgbtn.CommandName=strCommandName;
                  imgbtn.ID=strImageButtonName;
                  imgbtn.DataBinding+=new EventHandler(imgbtn_DataBinding);
                  imgbtn.Click +=new ImageClickEventHandler(imgbtn_Click);
                  objContainer.Controls.Add(imgbtn);
            }
            private void imgbtn_DataBinding(object sender, EventArgs e)
            {
                  ImageButton imgBtn = (ImageButton)sender;
                  imgBtn.ImageUrl=strImageURL;
                  imgBtn.Visible=Visibility;
                  imgBtn.CommandName=strCommandName;
                  imgBtn.CausesValidation=false;
            }
      }
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"));
            }
0
haritha_204
Asked:
haritha_204
1 Solution
 
wizard_340Commented:
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?
0
 
haritha_204Author Commented:
Hi,
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.
Thanks!!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now