我有GridView
个人在展示记录.我想根据底层记录的某些属性有条件地显示CommandField
或ButtonField
.其 idea 是只允许对特定的人执行命令.
最好的方法是什么?比起程序性的解决方案,我更喜欢声明性的解决方案.
我有GridView
个人在展示记录.我想根据底层记录的某些属性有条件地显示CommandField
或ButtonField
.其 idea 是只允许对特定的人执行命令.
最好的方法是什么?比起程序性的解决方案,我更喜欢声明性的解决方案.
首先,将ButtonField
或CommandField
转换为TemplateField
,然后将按钮的Visible
属性绑定到实现业务逻辑的方法:
<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")) %>'
CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后,在后面的代码中,添加以下方法:
protected Boolean IsOverAgeLimit(Decimal Age) {
return Age > 35M;
}
这里的优点是您可以相当容易地测试IsOverAgeLimit
方法.