开发者

Selecting an Item in GridView with ITemplate Generated Button

开发者 https://www.devze.com 2023-03-11 17:30 出处:网络
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.

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 ca开发者_如何学编程n I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号