1

我正在尝试使用以下代码切换 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);
    }
}

当我以 John Smith 身份登录时,我得到:

    +------+-------------------------------------+
    | Edit | John Smith                          |
    +------+-------------------------------------+
    | Edit | Jane Doe                            |
    +------+-------------------------------------+

启用 John Smith Edit 并禁用 Jane Doe 编辑。

我怎样才能让第一种方法起作用?

4

0 回答 0