Link to home
Start Free TrialLog in
Avatar of haritha_204
haritha_204

asked on

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"));
            }
ASKER CERTIFIED SOLUTION
Avatar of wizard_340
wizard_340

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of haritha_204
haritha_204

ASKER

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!!