【问题标题】:Kentico ascx transformation syntaxKentico ascx 转换语法
【发布时间】:2016-08-02 20:00:05
【问题描述】:

我从不同的地方拼凑了这个。我想要做的是显示或隐藏基于用户角色的自定义页面类型,以及来自管理员端的选项(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>

【问题讨论】:

  • 你可以尝试重写 OnLoad 方法而不是 OnDataBinding 吗?
  • 这杀死了页面。虽然不确定错误。

标签: c# kentico


【解决方案1】:

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

【讨论】:

  • 此使用权限的第一次迭代。它起作用了,直到需要有选择地向多个组展示它。我们注意到编辑只创建了一次项目,而不是组需要的那么多。我同意条件语句很混乱,但这里的 AD 角色并不干净。
【解决方案2】:

这似乎对我有用:

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

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

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

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

User is in role editors

{% } else { %}

user is not in editors role

{% }%}

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

【讨论】:

  • 您可能需要为 Richard 提供更多关于速度和效率的证据,因为 Kentico 已明确表示其中一个并不比另一个快。事实上,标准的 ascx 转换可以被认为更快,因为它不必通过宏引擎(无论如何都是 C# 代码)进行处理。另一件事是,如果您检查此转换,则可以编译 ascx 转换,而不能使用文本转换来编译。这对于升级非常方便。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多