34

私はasp.netGridViewを持っています:

<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の回答を、1つの小さな変更を加えて、自分のプロジェクトで使用することができました。ユーザーがクリックした行の特定のセルの値を取得する必要がありましたLinkButton。2行目は、必要な数のセルの値を取得するように変更できます。

これが私の解決策です:

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 に答える