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=tru e;
imgbtn.Click +=new ImageClickEventHandler(img btn_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 CreateItemTemplateImageBut ton(Header Text[i],Da taText[i], ImageURL[i ]);
this.dg.Columns.Add(TempCo l);
}
}
3. Implementing the ITemplate Interface:
public class CreateItemTemplateImageBut ton : ITemplate
{
string strImageURL;
string strImageButtonName;
string strCommandName;
bool Visibility = true;
public CreateItemTemplateImageBut ton(string ImageButtonName,string ImageUrl,string CommandName)
{
this.strImageURL =ImageUrl;
this.strImageButtonName=Im ageButtonN ame;
this.strCommandName=Comman dName;
}
public CreateItemTemplateImageBut ton(string ImageButtonName,string ImageUrl,string CommandName,bool Visibility)
{
this.strImageURL =ImageUrl;
this.strImageButtonName=Im ageButtonN ame;
this.Visibility=Visibility ;
this.strCommandName=Comman dName;
}
public void InstantiateIn(Control objContainer)
{
ImageButton imgbtn = new ImageButton();
imgbtn.CommandName=strComm andName;
imgbtn.ID=strImageButtonNa me;
imgbtn.DataBinding+=new EventHandler(imgbtn_DataBi nding);
imgbtn.Click +=new ImageClickEventHandler(img btn_Click) ;
objContainer.Controls.Add( imgbtn);
}
private void imgbtn_DataBinding(object sender, EventArgs e)
{
ImageButton imgBtn = (ImageButton)sender;
imgBtn.ImageUrl=strImageUR L;
imgBtn.Visible=Visibility;
imgBtn.CommandName=strComm andName;
imgBtn.CausesValidation=fa lse;
}
}
This is my Itemdatabound event code:
private void dgTest_ItemDataBound(objec t sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
imgbtn = ((ImageButton)e.Item.Cells [3].FindCo ntrol("img BtnID"));
}
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=tru
imgbtn.Click +=new ImageClickEventHandler(img
}
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 CreateItemTemplateImageBut
this.dg.Columns.Add(TempCo
}
}
3. Implementing the ITemplate Interface:
public class CreateItemTemplateImageBut
{
string strImageURL;
string strImageButtonName;
string strCommandName;
bool Visibility = true;
public CreateItemTemplateImageBut
{
this.strImageURL =ImageUrl;
this.strImageButtonName=Im
this.strCommandName=Comman
}
public CreateItemTemplateImageBut
{
this.strImageURL =ImageUrl;
this.strImageButtonName=Im
this.Visibility=Visibility
this.strCommandName=Comman
}
public void InstantiateIn(Control objContainer)
{
ImageButton imgbtn = new ImageButton();
imgbtn.CommandName=strComm
imgbtn.ID=strImageButtonNa
imgbtn.DataBinding+=new EventHandler(imgbtn_DataBi
imgbtn.Click +=new ImageClickEventHandler(img
objContainer.Controls.Add(
}
private void imgbtn_DataBinding(object sender, EventArgs e)
{
ImageButton imgBtn = (ImageButton)sender;
imgBtn.ImageUrl=strImageUR
imgBtn.Visible=Visibility;
imgBtn.CommandName=strComm
imgBtn.CausesValidation=fa
}
}
This is my Itemdatabound event code:
private void dgTest_ItemDataBound(objec
{
imgbtn = ((ImageButton)e.Item.Cells
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!!