禁用所有行的编辑按钮被点击GridView控件中的一个特定的行编辑按钮时, [英] Disable the Edit button on all the rows when Edit button is clicked on a particular row in GridView

查看:273
本文介绍了禁用所有行的编辑按钮被点击GridView控件中的一个特定的行编辑按钮时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5列与编辑GridView和删除的GridView控件的每一行按键。一旦编辑按钮被点击特定的行,我想禁用的编辑和行的其余部分删除按钮。

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 buttons on rest of the rows.

下面是我的aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" EnableModelValidation="True"          onrowdatabound="GridView1_RowDataBound" onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting" EditRowStyle-ForeColor="Aqua"  onrowediting="GridView1_RowEditing" >

<asp:TemplateField HeaderText="Item#" >
<ItemTemplate>
<asp:Label ID="Item#" runat="server" Text='Label' ></asp:Label>
</ItemTemplate>
  </asp:TemplateField>

      ..........  

  <asp:TemplateField >
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CausesValidation ="false" Font-Size="Smaller" CommandName="Edit" Text="Edit" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' Width="35px"/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField >
<ItemTemplate> 
 <asp:Button ID="btnDelete" runat="server" CssClass="GridButton" CausesValidation="False" CommandName="Delete" Font-Size="Smaller" Text="Delete" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
     if (e.CommandName == "Delete")
            {
                int x = Convert.ToInt32(e.CommandArgument.ToString());
                txtLineItemNumber.Text = (x + 1).ToString();
            }
            else
            {

               int x = Convert.ToInt32(e.CommandArgument.ToString());
               foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowIndex != x)
                    {
                        Button editButton = (Button)row.FindControl("btnEdit");
                        editButton.Enabled = false;
                        Button deleteButton = (Button)row.FindControl("btnDelete");
                        deleteButton.Enabled = false;
                    }
                    else
                    {
                        Button deleteButton = (Button)row.FindControl("btnDelete");
                        deleteButton.Text = "Cancel";
                        e.CommandName = "Cancel"; --- This is not possible...what should I do so that the Command Name is changed to Cancel and it does the cancel operation.
                    }

                }                 }
      }

请帮我得到它的工作.....

Please help me to get it working.....

推荐答案

您正在将正常工作的方法。然而这是在您的标记设置CommandArgument CommandArgument ='&LT;%#DataBinder.Eval的(集装箱的rowIndex)%&GT; 不是对按钮的引用itsself但在GridView排按钮位于的索引。

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.

您需要检索来自CommandArgument指数在RowCommand事件,然后启用/禁用其他行的所有按钮,就像这样:

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

修改(基于评论)

尝试将code,它修改了删除按钮,像这样的RowEditing事件处理程序:

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

希望这有助于。

这篇关于禁用所有行的编辑按钮被点击GridView控件中的一个特定的行编辑按钮时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆