Erick Garcia
Erick Garcia

Reputation: 842

Selecting an Item in GridView with ITemplate Generated Button

I'm having trouble figuring out how could I use my imagebutton (see below) in my ITemplate to append the button's corresponding row data (ItemID) as a query string.

My ImageButton in my ITemplate:

ImageButton select_button = new ImageButton();
select_button.ID = "select_button";
select_button.ImageUrl = "~/Files/System/Icons/highlighter.png";
select_button.CommandName = "Select";
select_button.ToolTip = "Select";
container.Controls.Add(select_button);

Should I handle it in in the imagebutton's OnClick event (if so, is there a way to get the row where the button is located) or can I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?

I'd be glad to elaborate more on my code upon request. ^ ^

Upvotes: 1

Views: 300

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You can set the ID in the CommandArgument property of your button control in the RowDataBound Event. Once you have an ID, you can track rows with it.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRow dr = ((DataRowView)e.Row.DataItem).Row;
        ((Button)e.Row.FindControl("select_button")).CommandArgument = dr["IdColumn"].ToString();
    }
}

Upvotes: 1

Related Questions