I want to create a GridView dynamic with a buttoncolumn in each row and do a Click at that ImageButton. This ImageButton have a commandargument filled in - and I whish to resspond to that commandargument When I click at the button and do something with this value.
I can easy run the method imgBtn_DataBind() - but not the others :-(
I can easy create the grid and add labels to it and create the ImageButton's too - but when I add some events to these buttons I can not get the debugger to stop in these routines :-(
So what do I do for accessing these methods and do something here? I really hope that there is an answer ;-)
Or do I need to consider a repeater?
Regards Michael
What I have tried:
I have the following code:
public class DynamicGridViewImageButtonTemplate : ITemplate
{
string _ColNameText;
DataControlRowType _rowType;
public DynamicGridViewImageButtonTemplate(string ColNameText, DataControlRowType RowType)
{
_ColNameText = ColNameText;
_rowType = RowType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (_rowType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = "" + _ColNameText + "";
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
ImageButton vImageButton = new ImageButton();
vImageButton.ID = "btnGetVoucher";
vImageButton.ImageUrl = "~/Images/Icons/icons8-download-from-the-cloud-50.png";
vImageButton.PostBackUrl = "#DownloadVoucher";
vImageButton.CommandArgument = string.Format("<%# Eval({0}) %>", "Voucher");
vImageButton.ToolTip = "Se og download bilag";
vImageButton.CssClass = "divButton";
vImageButton.Height = 25;
vImageButton.Width = 25;
vImageButton.DataBinding += new EventHandler(this.imgBtn_DataBind);
vImageButton.CommandName = "DownloadVoucher";
vImageButton.Command += new CommandEventHandler(btnGetVoucher_Command);
vImageButton.Click += new ImageClickEventHandler(btnGetVoucher_Click);
container.Controls.Add(vImageButton);
break;
default:
break;
}
}
private void imgBtn_DataBind(Object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
btn.CommandArgument = DataBinder.Eval(row.DataItem, _ColNameText).ToString();
}
protected void btnGetVoucher_Click(object sender, ImageClickEventArgs e)
{
}
protected void btnGetVoucher_Command(object sender, CommandEventArgs e)
{
ImageButton btn = sender as ImageButton;
}
}
And I have tried to call the methods and them to the GridView in this way (vGridView is the GridView()):
vTemplateField = new TemplateField();
vTemplateField.HeaderTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.Header);
vTemplateField.ItemTemplate = new DynamicGridViewImageButtonTemplate("Voucher", DataControlRowType.DataRow);
vGridView.Columns.Add(vTemplateField);