【问题标题】:How to get row data by clicking a button in a row in an ASP.NET gridview如何通过单击 ASP.NET gridview 中一行中的按钮来获取行数据
【发布时间】:2012-12-24 15:51:36
【问题描述】:

我在一个 ASP.NET Web 应用程序中有一个 GridView,我在其中的每一行中添加了两个按钮:

 <ItemTemplate>
    <asp:Button ID="btnEdit" Text="Edit" runat="server" />
    <asp:Button ID="btnDelete" Text="Delete" runat="server"/>
 </ItemTemplate>

现在如何通过单击一行中的编辑按钮从 gridview 获取行数据?

【问题讨论】:

    标签: c# asp.net button gridview


    【解决方案1】:

    你也可以像这样使用按钮点击事件:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="Button1" runat="server" Text="Button" 
                        OnClick="MyButtonClick" />
        </ItemTemplate>
    </asp:TemplateField>
    
    protected void MyButtonClick(object sender, System.EventArgs e)
    {
        //Get the button that raised the event
        Button btn = (Button)sender;
    
        //Get the row that contains this button
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    } 
    

    你可以这样做来获取数据:

     void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
     {
    
        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="Select")
        {
          // Convert the row index stored in the CommandArgument
          // property to an Integer.
          int index = Convert.ToInt32(e.CommandArgument);    
    
          // Get the last name of the selected author from the appropriate
          // cell in the GridView control.
          GridViewRow selectedRow = CustomersGridView.Rows[index];
        }
    }
    

    gridview 中的 Button 应该有这样的命令并处理 rowcommand 事件:

    <asp:gridview id="CustomersGridView" 
            datasourceid="CustomersSqlDataSource" 
            autogeneratecolumns="false"
            onrowcommand="CustomersGridView_RowCommand"
            runat="server">
    
            <columns>
              <asp:buttonfield buttontype="Button" 
                commandname="Select"
                headertext="Select Customer" 
                text="Select"/>
            </columns>
      </asp:gridview>
    

    Check full example on MSDN

    【讨论】:

    • thnx 再次快速回复。我试过了...它显示 ithis 错误“无效的回发或回调参数。事件验证在配置中使用 或 在页面中。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager。 RegisterForEventValidation 方法,以便注册回发或回调数据以进行验证。"
    • @TamalKantiDey - 嘿,只需设置 EnableEventValidation="false" 即可消除该错误
    • 我必须在哪里设置这个 EnableEventValidation 属性?在网格中我找不到任何这样的属性。
    • 在此标签顶部的页面中“
    • 谢谢它的工作,我的意思是没有错误出现。但是 _RowCommand 事件没有触发。
    【解决方案2】:

    commandName 放在.aspx 页面中

     <asp:Button  ID="btnDelete" Text="Delete" runat="server" CssClass="CoolButtons" CommandName="DeleteData"/>
    

    为网格订阅rowCommand事件,你可以这样尝试,

    protected void grdBillingdata_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if (e.CommandName == "DeleteData")
            {
                GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                HiddenField hdnDataId = (HiddenField)row.FindControl("hdnDataId");
             }
    }
    

    【讨论】:

    • 这还不错,只是你忘了注明hdnDataId 的来源。
    【解决方案3】:
    <ItemTemplate>
         <asp:Button ID="Button1" runat="server" Text="Button" 
                OnClick="MyButtonClick" />
    </ItemTemplate>
    

    你的方法

     protected void MyButtonClick(object sender, System.EventArgs e)
    {
         //Get the button that raised the event
    Button btn = (Button)sender;
    
        //Get the row that contains this button
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    }
    

    【讨论】:

    • 这似乎与 Pranay Rana 的回答完全相同。
    【解决方案4】:

    您是否有任何特定原因希望您在项目模板中使用按钮。您也可以通过以下方式进行操作,从而为您提供网格行编辑事件的全部功能。您还可以获得轻松接线的奖励取消和删除功能。

    标记

    <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
       <asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
        ImageUrl="~/images/Edit.png" Style="height: 16px" ToolTip="Edit" 
        CausesValidation="False"  />
    
          </ItemTemplate>
    
             <EditItemTemplate>
    
                        <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" 
                            Text="Update"  Visible="true" ImageUrl="~/images/saveHS.png" 
                            />
                       <asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel"   
                            ImageUrl="~/images/Edit_UndoHS.png"  />
    
                     <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete"   
                            ImageUrl="~/images/delete.png"  />
    
                 </EditItemTemplate>
    
    
            <ControlStyle BackColor="Transparent" BorderStyle="None" />
                   <FooterStyle HorizontalAlign="Center" />
               <ItemStyle HorizontalAlign="Center" />
           </asp:TemplateField>
    

    背后的代码

     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    
    TextBox txtledName =   (TextBox) GridView1.Rows[e.NewEditIndex].FindControl("txtAccountName");
    
     //then do something with the retrieved textbox's text.
    
    }
    

    【讨论】:

      【解决方案5】:
      <asp:TemplateField>
         <ItemTemplate>
        <asp:LinkButton runat="server" ID="LnKB" Text='edit' OnClick="LnKB_Click"   > 
       </asp:LinkButton>
       </ItemTemplate>
      </asp:TemplateField>
      
        protected void LnKB_Click(object sender, System.EventArgs e)
        {
              LinkButton lb = sender as LinkButton;
      
              GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
              int x = clickedRow.RowIndex;
              int id = Convert.ToInt32(yourgridviewname.Rows[x].Cells[0].Text);
              lbl.Text = yourgridviewname.Rows[x].Cells[2].Text; 
         }
      

      【讨论】:

        【解决方案6】:
                    <asp:Button  ID="btnEdit" Text="Edit" runat="server"  OnClick="btnEdit_Click" CssClass="CoolButtons"/>
        
        
        protected void btnEdit_Click(object sender, EventArgs e)
        {
               Button btnEdit = (Button)sender;
               GridViewRow Grow = (GridViewRow)btnEdit.NamingContainer;
              TextBox txtledName = (TextBox)Grow.FindControl("txtAccountName");
              HyperLink HplnkDr = (HyperLink)Grow.FindControl("HplnkDr");
              TextBox txtnarration = (TextBox)Grow.FindControl("txtnarration");
             //Get the gridview Row Details
        }
        

        与删除按钮相同

        【讨论】:

          【解决方案7】:
           protected void btnS10_click(object sender, EventArgs e)
              {
                  foreach (GridViewRow row in Grd.Rows)
                  {
                      CheckBox chk_Single = (CheckBox)row.FindControl("ChkSendOne");
                      if (row.RowType == DataControlRowType.DataRow)
                      {
                          string id = (row.Cells[0].FindControl("lblSNo") as Label).Text;
                          if (Convert.ToInt32(id) <= 10)
                          {
                             
                              chk_Single.Checked = true;
                              if (chk_Single.Checked == true)
                              {
                                  lblSelectedRecord.InnerText = (Convert.ToInt32(lblSelectedRecord.InnerText) + 1).ToString();
                              }
                          }
                      }
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-17
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            • 2015-01-28
            相关资源
            最近更新 更多