从gridview asp.net中的某一行禁用/灰显链接按钮

时间:2016-06-26 14:09:40

标签: c# asp.net gridview asplinkbutton

我想禁用/将其置于只读模式或任何使其无法永久运行的模式。我有这个:

 <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("ProductID") %>' />
            </ItemTemplate>
        </asp:TemplateField>

它看起来像这样: example image

现在我想要的是当我点击批准链接按钮时,它将变灰或失去其功能。 但仅限于我点击的行。如何才能做到这一点?顺便说一下,这是我背后的代码:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);

                if (chkRow.Checked)
                {
                    using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
                    {
                        scn.Open();
                        SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE o.CustomerID=@CustomerID", scn);
                        cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int).Value = row.Cells[0].Text;
                        cmd.ExecuteNonQuery();
                        Label1.Visible = true;
                        Label2.Visible = false;
                        GridView1.DataBind();
                    }

                }
            }
        }
    }

更新:

根据我提供的图像,我认为如果可能的话,整个行也会变灰。点击墨水按钮后它就好像无用了

1 个答案:

答案 0 :(得分:0)

你必须找到你的链接按钮并禁用它,如下所示:

var lb = row.FindControl("btnApprove") as LinkButton;
//var lb = row.Cells[0].Controls[0] as LinkButton;
lb.Enabled = false;
lb.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
lb.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";

实际上你必须找到行内的复选框。 您还可以向该行添加样式以使其看起来处于禁用状态:

 row.CssClass = "row-disabled";

<style>
    tr.row-disabled{
       color:gray;
    }
</style>