【问题标题】:How do I disable LinkButton in gridview based on a value from another column?如何根据另一列中的值禁用 gridview 中的 LinkBut​​ton?
【发布时间】:2020-02-15 09:57:58
【问题描述】:

我有一个状态列,其中包括已批准、已拒绝或已取消。我有另一个包含链接按钮批准和拒绝的操作列。现在,如果状态为 Cancelled,我希望禁用该行的链接按钮。

我尝试使用 GridView1_DataBound 事件但无法弄清楚逻辑。

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {


                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Server.HtmlDecode(e.Row.Cells[0].Text.Trim()).Equals("Cancelled"))
                    {

                        //OR you can disable it instead of complately hiding it
                        ((LinkButton)GridView1.Rows[e.Row.RowIndex].Cells[0].Controls[0]).Enabled = false;
                    }
                }
}

【问题讨论】:

  • 所以你说上面的代码不起作用,对吗?你做了哪些调试来尝试确定它开始出错的地方?
  • P.S.在一个地方你使用e.Row.Cells 来识别行,而在另一个地方你(似乎是随机的)使用更长的GridView1.Rows[e.Row.RowIndex].Cells 来做同样的工作。为什么要让它变得更复杂?通过简单的方法使您的代码保持一致、更短、更易于阅读。
  • GridView 中使用了什么样的对象?就像,如果我的 GridView.DataSource = Collection(Of Products),那么在 RowDataBound 上我正在处理 1 个单独的产品。所以我会将该行转换为产品对象,然后我可以轻松访问产品属性以查看它是否已被取消。
  • @JohnPete22 好主意。但我假设您的意思是您将投射行的DataItem 而不是行本身? :-)
  • @ADyson 是的,这是正确的。抱歉,有时我忘记了我需要更加具体。例如Product item = (Product)e.Row.DataItem

标签: c# asp.net gridview webforms


【解决方案1】:

方法A

以下应该有效:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="Status" DataField="Status" />
        <asp:TemplateField HeaderText="Actions">
            <ItemTemplate>
                <asp:LinkButton runat="server" Enabled='<%# !(Eval("Status").ToString().Equals("Cancelled")) %>'>Approve</asp:LinkButton>
                <asp:LinkButton runat="server" Enabled='<%# !(Eval("Status").ToString().Equals("Cancelled")) %>'>Reject</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

方法B

尽管如此,如果您坚持使用代码隐藏方法,访问 LinkButton 控件的最安全方法如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Status" DataField="Status" />
        <asp:TemplateField HeaderText="Actions">
            <ItemTemplate>
                <asp:LinkButton ID="ApproveButton" runat="server">Approve</asp:LinkButton>
                <asp:LinkButton ID="RejectButton" runat="server">Reject</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[0].Text.Equals("Cancelled"))
        {
            ((LinkButton)e.Row.FindControl("ApproveButton")).Enabled = false;
            ((LinkButton)e.Row.FindControl("RejectButton")).Enabled = false;
        }
    }
}

说明

您的代码不起作用,因为LinkButton 控件未按照您期望的方式放置在单元格的Controls 集合中。通过在GridView1_RowDataBound 的内部条件中放置一个断点来找出自己,并检查Controls 集合的项目。你会感到惊讶!

【讨论】:

    【解决方案2】:

    也许你可以使用这个代码

    <asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridView_RowDataBound">
        <Columns>
            <asp:BoundField HeaderText="Column1" DataField="Column1" />
            <asp:BoundField HeaderText="Column2" DataField="Column2" />
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblStatus" Text='<%#Bind("Status") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Actions">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkAction" runat="server">Approve</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var status = (e.Row.FindControl("lblStatus") as Label).Text;
            if (status == "Cancelled")
            {
                (e.Row.FindControl("lnkAction") as LinkButton).DisableControl("Disabled button.");
            }
        }
    }
    

    要禁用按钮,您可以使用扩展方法

    public static class Extensions
    {
          public static TControl DisableControl<TControl>(this TControl control, string desableMessage) where TControl : WebControl
        {
            control.Attributes.Add("disabled", "");
            control.Attributes.Add("data-toggle", "tooltip");
            control.Attributes.Add("title", disableMessage);
            control.Enabled = false;
            return control;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多