Mark Handy
Mark Handy

Reputation: 1256

Kentico ascx transformation syntax

I've cobbled this together from various places. What i'm trying to do is show or hide a custom page type based on a user role, and a option from the admin side (visibiltyType).

So far it seems to be working, but i'm not sure of my conditional statement syntax is the best. C# is not my strong point.

Here's my transformation:

<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>

Upvotes: 0

Views: 781

Answers (2)

Roman Hutnyk
Roman Hutnyk

Reputation: 1549

You may configure Page Type permissions and avoid all that checks in your transformation.

Upvotes: 1

Enn
Enn

Reputation: 2209

This seems to work on my end:

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

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

However in your case I would recommend to switch to Text/XML transformation because its more efficient/faster and it will be much easer for you to write such conditions. In your case it would be as nice as writing this:

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

User is in role editors

{% } else { %}

user is not in editors role

{% }%}

It can of course be more complex and you could even write a custom macro method for this purpose, but you get the idea :)

Upvotes: 0

Related Questions