Kentico ascx转换语法

时间:2016-05-09 13:53:34

标签: c# kentico

我从各个地方拼凑了这个。我试图做的是显示或隐藏基于用户角色的自定义页面类型,以及来自管理端的选项(visibiltyType)。

到目前为止似乎工作正常,但我不确定我的条件语句语法是否最好。 C#不是我的强项。

这是我的转变:



<script runat="server">
  public bool visibility;
  public string visiblityType;
  protected override void OnDataBinding(EventArgs e)
    {
      visiblityType = Eval("Visibility").ToString();
      if( CMS.Membership.MembershipContext.AuthenticatedUser.IsInRole("scona-cms_resources_branchdirectors", CMS.SiteProvider.SiteContext.CurrentSiteName) && visiblityType == "ncp" ){
        alert.Visible = true;  
      }
      if( CMS.Membership.MembershipContext.AuthenticatedUser.IsInRole("scona-cms_resources_salesreps", CMS.SiteProvider.SiteContext.CurrentSiteName) && visiblityType == "ncp" ){
        alert.Visible = true;  
      }
      if( CMS.Membership.MembershipContext.AuthenticatedUser.IsInRole("scona-kff-headoffice", CMS.SiteProvider.SiteContext.CurrentSiteName) && visiblityType == "ncp" ){
        alert.Visible = true;  
      }      
      if( CMS.Membership.MembershipContext.AuthenticatedUser.IsInRole("scona-cms_resources_partnernetwork", CMS.SiteProvider.SiteContext.CurrentSiteName) && visiblityType == "cp" ){
        alert.Visible = true;  
      }      
      if (visiblityType == "both"){
        alert.Visible = true;
      }     
      
    }
</script>
<asp:placeholder id="alert" runat="server" Visible="false">  
  <li><%# Eval("Visibility") %> | <%# Eval("AlertDate") %> - <%# Eval("AlertTitle") %> <%# IfEmpty(Eval("AlertCopy"),"", " <a href='" +  GetDocumentUrl() + "'>Read More</a>")  %></li>
</asp:placeholder>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以配置页面类型权限,并避免转换中的所有检查。

答案 1 :(得分:0)

这似乎对我有用:

<script runat="server">
  protected override void OnLoad(EventArgs e)
    {
      alert.Visible = true;
    }
 </script>

<asp:placeholder id="alert" runat="server" visible="false">
  Placeholder
</asp:placeholder>

但是在我的情况下,我会建议切换到文本/ XML 转换,因为它的更高效/更快,您可以更轻松地编写此类条件。在你的情况下,它会像写这个一样好:

{% if(CurrentUser.IsInRole("Editors")){ %}

User is in role editors

{% } else { %}

user is not in editors role

{% }%}

它当然可以更复杂,您甚至可以为此目的编写自定义宏方法,但您明白了这一点:)