Click here to Skip to main content
14,837,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 + "";
            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);

    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);
Updated 1-Mar-21 9:22am
Gerry Schmitz 2-Mar-21 14:05pm
Maybe you should have one button that uses the selected row data to do whatever instead of adding a button to every row that does the same thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900