我有GridView
个人显示记录。我想根据基础记录的某些属性有条件地显示CommandField
或ButtonField
。我们的想法是只允许对特定人员执行命令。
最好的方法是什么?我更喜欢程序性的声明性解决方案。
答案 0 :(得分:56)
首先,将您的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
方法。
答案 1 :(得分:12)
可以在RowDataBound
事件触发
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Hide the edit button when some condition is true
// for example, the row contains a certain property
if (someCondition)
{
Button btnEdit = (Button)e.Row.FindControl("btnEdit");
btnEdit.Visible = false;
}
}
}
这是一个演示页
标记
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DropDownDemo._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridView OnRowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Name" DataField="name" />
<asp:BoundField HeaderText="Age" DataField="age" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnEdit" runat="server" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
背后的代码
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetCustomers();
GridView1.DataBind();
}
protected override void OnInit(EventArgs e)
{
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
base.OnInit(e);
}
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow) return;
int age;
if (int.TryParse(e.Row.Cells[1].Text, out age))
if (age == 30)
{
Button btnEdit = (Button) e.Row.FindControl("btnEdit");
btnEdit.Visible = false;
}
}
private static List<Customer> GetCustomers()
{
List<Customer> results = new List<Customer>();
results.Add(new Customer("Steve", 30));
results.Add(new Customer("Brian", 40));
results.Add(new Customer("Dave", 50));
results.Add(new Customer("Bill", 25));
results.Add(new Customer("Rich", 22));
results.Add(new Customer("Bert", 30));
return results;
}
}
public class Customer
{
public string Name {get;set;}
public int Age { get; set; }
public Customer(string name, int age)
{
Name = name;
Age = age;
}
}
}
在演示中,在客户年龄为30岁的行中,编辑按钮不是可见(HTML标记不会发送到客户端)。
答案 2 :(得分:10)
请允许我分享我的方法,了解它的价值。对于我来说,将命令字段转换为模板字段控件不是一个选项,因为命令字段带有内置功能,否则我必须自己创建,例如它更改为&#34;更新取消&#34;单击“编辑”时,单击“编辑”时,行中作为标签的所有单元格将成为文本框等。
在我的方法中,您可以按原样保留命令字段,然后您可以根据需要通过后面的代码隐藏它。在这个例子中,如果字段&#34; Scenario&#34;网格显示文本&#34; Actual&#34;对于RowDataBound事件的相关行。
protected void gridDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((Label)e.Row.FindControl("lblScenario")).Text == "Actual")
{
LinkButton cmdField= (LinkButton)e.Row.Cells[0].Controls[0];
cmdField.Visible = false;
}
}}
答案 3 :(得分:6)
如果要从表中完全删除列(即不仅仅是按钮),请使用合适的事件处理程序,例如:对于OnDataBound
事件,然后隐藏目标GridView
上的相应列。选择一个仅为此控件触发一次的事件,即不是OnRowDataBound
。
<asp:GridView ID="grdUsers" runat="server" DataSourceID="dsProjectUsers" OnDataBound="grdUsers_DataBound">
<Columns>
<asp:TemplateField HeaderText="Admin Actions">
<ItemTemplate><asp:Button ID="btnEdit" runat="server" text="Edit" /></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Telephone" HeaderText="Telephone" />
</Columns>
</asp:GridView>
protected void grdUsers_DataBound(object sender, EventArgs e)
{
try
{
// in this case hiding the first col if not admin
if (!User.IsInRole(Constants.Role_Name_Admin))
grdUsers.Columns[0].Visible = false;
}
catch (Exception ex)
{
// deal with ex
}
}
答案 4 :(得分:1)
将CommandField转换为TemplateField,并根据字段的值设置按钮的visible属性(true / false)
<asp:Button ID="btnSelect"
runat="server" Text="Select"
Visible='<%# DataBinder.Eval(Container.DataItem,"IsLeaf") %>'/>
答案 5 :(得分:1)
要有条件地控制模板/命令字段的视图,请使用Gridview的RowDataBound事件,如:
<asp:GridView ID="gv1" OnRowDataBound="gv1_RowDataBound"
runat="server" AutoGenerateColumns="False" DataKeyNames="Id" >
<Columns>
...
<asp:TemplateField HeaderText="Order Status"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblOrderStatus" runat="server"
Text='<%# Bind("OrderStatus") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
...
<asp:CommandField ShowSelectButton="True" SelectText="Select" />
</Columns>
</asp:GridView>
以下:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label lblOrderStatus=(Label) e.Row.Cells[4].FindControl("lblOrderStatus");
if (lblOrderStatus.Text== "Ordered")
{
lblOrderStatus.ForeColor = System.Drawing.Color.DarkBlue;
LinkButton bt = (LinkButton)e.Row.Cells[5].Controls[0];
bt.Visible = false;
e.Row.BackColor = System.Drawing.Color.LightGray;
}
}
答案 6 :(得分:1)
<asp:GridView ID="gv_Document" CssClass="gridstyle" runat="server" OnRowDataBound="gv_Document_RowDataBound" AutoGenerateColumns="false" DataKeyNames="SourceGUID,Source,FilePath" ShowHeaderWhenEmpty="false" OnRowDeleting="gv_Document_RowDeleting">
<Columns>
<asp:BoundField HeaderText="ItemID" DataField="ItemID" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:BoundField HeaderText="SourceGUID" DataField="SourceGUID" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:BoundField HeaderText="Source" DataField="Source" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="hyperLink" runat="server" Target="_blank" NavigateUrl='<%# Bind("FilePath")%>'
Text='<%# Bind("FileName")%>'> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Type" DataField="FileExtension" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:BoundField HeaderText="Content type" DataField="FileMimeType" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:BoundField HeaderText="File Path" DataField="FilePath" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" />
<asp:CommandField ShowDeleteButton="True" DeleteText="Delete" />
</Columns>
使用此代码可从后面的代码禁用gridview中的删除按钮。
protected void gv_Document_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[7].Controls[0]).Visible = false;
}
}
答案 7 :(得分:0)
如果这是基于角色,您可以使用多视图面板但不确定是否可以对记录的属性执行相同的操作。
但是,您可以通过代码执行此操作。在rowdatabound事件中,您可以隐藏或显示其中的按钮。
答案 8 :(得分:0)
您可以根据GridView中的位置(索引)隐藏CommandField或ButtonField。
例如,如果您的CommandField位于第一个位置(index = 0),您可以隐藏它在GridView的事件RowDataBound中添加以下代码:
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
答案 9 :(得分:0)
我做了一件非常简单的事情来启用或禁用命令按钮。下面是我的网格
build
在以下方法中,我完成了更改
<asp:GridView ID="grdOrderProduct" runat="server" TabIndex="1" BackColor="White" BorderColor="#CEC9EF" CssClass="table table-striped dataTable table-bordered"
OnRowEditing="grdOrderProduct_RowEditing" OnRowUpdating="grdOrderProduct_RowUpdating" OnRowDeleting="grdOrderProduct_RowDeleting" OnRowDataBound="grdOrderProduct_RowDataBound"
Width="100%" CellPadding="3" CellSpacing="1" BorderWidth="0" AutoGenerateColumns="False">
<HeaderStyle />
<AlternatingRowStyle />
<Columns>
<asp:BoundField DataField="ProductSKU" ReadOnly="true" HeaderText="Product SKU" HeaderStyle-CssClass="headTb4" />
<asp:BoundField DataField="ProductName" ReadOnly="true" HeaderText="ProductName" HeaderStyle-CssClass="headTb4" />
<asp:BoundField DataField="QTY" HeaderText="QTY" HeaderStyle-CssClass="headTb4" />
<asp:BoundField DataField="Discount" HeaderText="Discount %" HeaderStyle-CssClass="headTb4" />
<asp:BoundField DataField="TPrice" HeaderText="MRP" ReadOnly="true" HeaderStyle-CssClass="headTb4" />
<asp:CommandField ShowEditButton="true" ButtonType="Image" EditImageUrl="~/Images/edit.png"
UpdateImageUrl="~/Images/gear.png" CancelText=" " HeaderStyle-CssClass="headTb4"
ShowDeleteButton="true" DeleteImageUrl="~/Images/delete.png"
HeaderText="Action" ItemStyle-HorizontalAlign="Center">
<HeaderStyle CssClass="headTb4" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
</Columns>
<AlternatingRowStyle CssClass="odd" />
<PagerStyle HorizontalAlign="Center" VerticalAlign="Top" Wrap="False" />