【问题标题】:Selecting an Item in GridView with ITemplate Generated Button使用 ITemplate 生成按钮在 GridView 中选择项目
【发布时间】:2011-06-07 04:22:31
【问题描述】:

我无法弄清楚如何在 ITemplate 中使用我的 imagebutton(见下文)将按钮的相应行数据 (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、rowseted、rowcommand 等) ?

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

【问题讨论】:

    标签: c# asp.net gridview itemplate


    【解决方案1】:

    您可以在RowDataBound 事件中的按钮控件的CommandArgument 属性中设置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();
        }
    }
    

    【讨论】:

    • 谢谢,我可以用你给我的例子在我的页面中添加 id 作为命令参数^ ^
    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 2010-11-28
    • 2012-08-06
    • 1970-01-01
    • 2013-07-27
    • 2021-07-03
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多