我有一个名为DisabledControl的用户控件。我需要使用这两个条件创建1 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.
答案 0 :(得分:1)
您无法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;
}