有条件隐藏CommandField中或ButtonField字段中的GridView [英] Conditionally hide CommandField or ButtonField in Gridview

查看:1335
本文介绍了有条件隐藏CommandField中或ButtonField字段中的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件显示人的记录。我想有条件地显示一个 CommandField中 ButtonField字段基于底层记录的某些属性。我们的想法是只允许特定的人执行的命令。

I have a GridView displaying person records. I want to conditionally show a CommandField or ButtonField based on some property of the underlying record. The idea is to only allow a command to be performed on specific people.

什么是做到这一点的最好方法是什么?我想preFER声明性解决方案,以程序性的。

What is the best way to do this? I'd prefer a declarative solution to a procedural one.

推荐答案

首先,将您 ButtonField字段 CommandField中模板列,然后绑定按钮来实现业务逻辑的方法可见属性:

First, convert your ButtonField or CommandField to a TemplateField, then bind the Visible property of the button to a method that implements the business logic:

<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" Text="Reject" 
                Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后,在code后面,添加在该方法

Then, in the code behind, add in the method:

protected Boolean IsOverAgeLimit(Decimal Age) {
    return Age > 35M;
}

这里的好处是你可以很容易测试 IsOverAgeLimit 方法。

这篇关于有条件隐藏CommandField中或ButtonField字段中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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