我试图使用以下代码切换CommandField.ShowEditButton是真还是假:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
clsEmployee employee = (clsEmployee)e.Row.DataItem;
CommandField cmdField = (CommandField)((DataControlFieldCell)e.Row.Cells[0]).ContainingField;
cmdField.ShowEditButton = _currentUser.FullName.Equals(employee.FullName);
}
}
它有效,但在进行中;不是现在的。如果我以John Smith身份登录,则会发生这种情况:
+------+-------------------------------------+
| | John Smith |
+------+-------------------------------------+
| Edit | Jane Doe |
+------+-------------------------------------+
我知道我在正确的行上,因为这有效(对于CommandField,ShowEditButton始终为true):
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
clsEmployee employee = (clsEmployee)e.Row.DataItem;
e.Row.Cells[0].Enabled = _currentUser.FullName.Equals(employee.FullName);
}
}
当我以约翰史密斯身份登录时,我得到:
+------+-------------------------------------+
| Edit | John Smith |
+------+-------------------------------------+
| Edit | Jane Doe |
+------+-------------------------------------+
启用John Smith Edit并禁用Jane Doe。
如何让第一种方法起作用?