使用ITemplate生成的按钮在GridView中选择项目

时间:2011-06-07 04:22:32

标签: c# asp.net gridview itemplate

我无法弄清楚如何在我的ITemplate中使用我的图像按钮(见下文)将按钮的相应行数据(ItemID)作为查询字符串附加。

我的ITemplate中的我的ImageButton:

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);

我应该在imagebutton的OnClick事件中处理它(如果是这样,有没有办法获取按钮所在的行)或者我可以处理GridView事件(rowbinding,rowseleted,rowcommand等) ?

我很乐意根据要求详细说明我的代码。 ^ ^

1 个答案:

答案 0 :(得分:1)

您可以在CommandArgument事件中的按钮控件的RowDataBound属性中设置ID。获得ID后,您可以使用它跟踪行。

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();
    }
}