Disable the Edit button on all the rows when Edit button is clicked on a particular row in GridView

前端 未结 2 867
独厮守ぢ
独厮守ぢ 2021-01-15 21:03

I have a gridView with 5 columns with Edit and Delete button on each row of GridView. Once an Edit button a particular row is clicked , I want to disable the edit and delete

相关标签:
2条回答
  • 2021-01-15 21:52

    You could use javascript, outlined here, http://www.krissteele.net/blogdetails.aspx?id=92 . Add logic to not disable for the current row.

    0 讨论(0)
  • 2021-01-15 21:54

    The approach you are taking will work fine. However the CommandArgument which is set in your markup CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' is not a reference to the Button itsself but the index of the GridView row the Button resides in.

    You will need to retrieve the index from the CommandArgument in the RowCommand event and then enable/disable all buttons on other rows like so:

    void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName.ToLower())
        {
            case "edit":
                {
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
    
                    foreach (GridViewRow row in gridView1.Rows)
                    {
                        if (row.RowIndex != rowIndex)
                        {
                            Button editButton = (Button)row.FindControl("btnEdit");
                            editButton.Enabled = false;
                            Button deleteButton = (Button)row.FindControl("btnDelete");
                            deleteButton.Enabled = false;
                        }
                    }
                }break;
        }
    }
    

    Edit (based on comment)

    Try moving the code that modifies the delete button to the RowEditing event handler like so:

    void gridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gridView1.EditIndex = e.NewEditIndex;
    
        DataBind();
    
        Button deleteButton = (Button)gridView1.Rows[e.NewEditIndex].FindControl("btnDelete");
        deleteButton.Text = "Cancel";
        deleteButton.CommandName = "Cancel";
    }
    
    void gridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gridView1.EditIndex = -1;
    
        DataBind();
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题