How to get row data by clicking a button in a row in an ASP.NET gridview

前端 未结 7 1643
情深已故
情深已故 2020-12-09 08:56

I have a GridView in a ASP.NET web application, in which I have added two buttons in each row:

 

        
相关标签:
7条回答
  • 2020-12-09 09:09

    Place the commandName in .aspx page

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

    Subscribe the rowCommand event for the grid and you can try like this,

    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");
             }
    }
    
    0 讨论(0)
  • 2020-12-09 09:11
                <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
    }
    

    And Same As for Delete button

    0 讨论(0)
  • 2020-12-09 09:13

    You can also use button click event like this:

    <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;
    } 
    

    OR

    You can do like this to get data:

     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];
        }
    }
    

    and Button in gridview should have command like this and handle rowcommand event:

    <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

    0 讨论(0)
  • 2020-12-09 09:18
     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();
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-09 09:22
    <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; 
       }
    
    0 讨论(0)
  • 2020-12-09 09:23

    Is there any specific reason you would want your buttons in an item template.You can alternatively do it the following way , there by giving you the full power of the grid row editing event.You are also given a bonus of wiring easily the cancel and delete functionality.

    Mark up

    <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>
    

    Code behind

     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.
    
    }
    
    0 讨论(0)
提交回复
热议问题