【问题标题】:Get Row Index on Asp.net Rowcommand event在 Asp.net Rowcommand 事件中获取行索引
【发布时间】:2011-09-24 02:30:37
【问题描述】:

我有一个 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

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

【问题讨论】:

    标签: c# asp.net gridview rowcommand


    【解决方案1】:

    这是您问题的答案。

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

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

        这是我的解决方案:

        GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        string typecore = gvr.Cells[3].Text.ToString().Trim();
        

        【讨论】:

          【解决方案4】:
          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)
              {
          
              }
          }
          

          【讨论】:

            【解决方案5】:

            或者,您可以使用 control 类代替它们的类型:

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

            【讨论】:

              【解决方案6】:

              图像按钮\按钮等

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

              代码隐藏

              protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
              {
                  int index = e.CommandArgument;
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-08-10
                • 1970-01-01
                • 2016-10-11
                • 2017-03-07
                相关资源
                最近更新 更多