基于多个用户条件启用的按钮

本文关键字:按钮 启用 条件 于多个 用户 | 更新日期: 2023-09-27 18:00:05

我有一个名为DisabledControl的用户控件。我需要用这两个条件创建一个if语句。

public static class DisabledControl
{
    public static void AdminOnly(this Control control)
    {
        control.Enabled = User.IsInRole(new string[] { "Administrator" });
    }
    public static void OwnerOnly(this Control control, int ownerID)
    {
        control.Enabled = (User.ID == ownerID);
    }
}
//I need help with syntax: if User isinRole 'Adminstrator" && user is OwnerOnly, btnSave.Enabled=true. 
//I tried to use if user.id == OwnerOnly, it gives error.

基于多个用户条件启用的按钮

不能执行user.id == OwnerOnly,因为OwnerOnly是一个返回void的方法。您可能需要user.id == ownerID,或者更改OwnerOnly以返回boolean

作为对您评论的回应,请尝试以下操作:

public static void SetEnabledForAdminOrOwner(this Control control, int ownerID)
{
    control.Enabled = User.IsInRole(new string[] { "Administrator" }) || User.ID == ownerID;
}