34

我有一个 asp.net GridView:

<asp:TemplateField HeaderText="View Faktor" ShowHeader="False" Visible="True">
    <ItemTemplate>
        <asp:ImageButton ID="imgBtn1" CssClass="SelectRow" runat="server" CausesValidation="false"
            CommandArgument='<%#(eval("mprID")) %>' CommandName="ViewFactors" ImageUrl="~/tadarokat/Images/factor.png"
            Text="" />
    </ItemTemplate>
</asp:TemplateField>

我怎样才能rowIndex参加行命令事件?

我想在触发select时突出显示 ( ) 目标行。RowCommand

4

6 回答 6

73

这是您问题的答案。

GridViewRow gvr = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;

int RowIndex = gvr.RowIndex; 
于 2011-06-28T08:11:26.720 回答
13

ImageButton \ 按钮等

CommandArgument='<%# Container.DataItemIndex%>' 

代码隐藏

protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = e.CommandArgument;
}
于 2014-03-11T17:45:19.453 回答
4

如果您有 GridView 的内置命令,如插入、更新或删除,在行命令上,您可以使用以下代码获取索引:

int index = Convert.ToInt32(e.CommandArgument);

在自定义命令中,您可以将命令参数设置为yourRow.RowIndex.ToString(),然后在 RowCommand 事件处理程序中将其取回。当然,除非您出于其他目的需要命令参数。

于 2011-08-01T17:47:32.210 回答
3

或者,您可以使用control类而不是它们的类型:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

int RowIndex = row.RowIndex; 
于 2017-07-22T16:51:34.410 回答
2

我能够在我自己的项目中使用上面的@rahularyansharma 的答案,只需稍作修改。我需要获取用户单击的行上特定单元格的值 a LinkButton。可以修改第二行以获得任意数量的单元格的值。

这是我的解决方案:

GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string typecore = gvr.Cells[3].Text.ToString().Trim();
于 2015-07-27T13:04:34.040 回答
1
protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Delete")
        {
            GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            int RemoveAt = gvr.RowIndex;
            DataTable dt = new DataTable();
            dt = (DataTable)ViewState["Products"];
            dt.Rows.RemoveAt(RemoveAt);
            dt.AcceptChanges();
            ViewState["Products"] = dt;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}
protected void gvProductsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        gvProductsList.DataSource = ViewState["Products"];
        gvProductsList.DataBind();
    }
    catch (Exception ex)
    {

    }
}
于 2012-08-04T10:52:13.827 回答