Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Darren 12 posts 32 karma points
    Jan 20, 2012 @ 11:35
    Darren
    0

    Is there an easy way to hide / show content for a specific user

    Hi, I'm new to Umbraco and have been given the task of importing a asp.net web app in umbraco. 

    I have not done this in VS2008 and ported it to Umbraco, as some posts have, but I have done this like this link (http://vaideeswaranr.blogspot.com/2010/11/my-first-page-with-umbraco.html), I'm not sure if this was the correct way to do it now, but I guess I did it like this was so that the content could be updated easily.  It seems to work well up to a point, possibly too late to change the whole thing now anyway!!.

    My Site has 6 content pages, with a 2 templates (1 with the the code for haed/body, linked to a css file and another with just a placeholder in it - which has a Document type linked to it (which contains a richText editor)

    However, the only issue I have left to resolve is regarding a user login Button that is in the asp.net site, it is positioned on the master page (right margin) that basically hides certain parts of other pages on the site, the button justs sets a session variable to true/false and then on a page in the site this is checked and content is either shown or hidden (content to be shown/hidden, such as document downloads, is done via an asp.net panel - so setting the visible property) - not eligant but seems to work.

    However in my umbraco I don't have a code behind page for the master page.  I have tried creating one by importing the Umbraco solution into VS2008 but I'm getting compilation errors!.

    Rather then bang my head against the wall by trying to create a code behind page in asp.net, Is there an 'Umbraco' way to easily hide certain parts of each content page for a specific user?. 

    FYI: I have also looked at creating a custom control and linking it via a macro, but I'm not sure how I would link thsi to hiding or showing content on my content pages!.

    Any help would be very much appreciated :-)

     

     

     

  • Rodion Novoselov 694 posts 859 karma points
    Jan 20, 2012 @ 11:51
    Rodion Novoselov
    0

    Hi. If you're really in need of adding some code to a master page (aka template) you can add it straight to the page with <script runat="server"> tag: http://msdn.microsoft.com/en-us/library/015103yb.aspx. And actually I cannot see any reason why code-behind model wouldn't work - you only need to use "CodeFile=..." instead of "CodeBehind=..." in the "@Master" directive, inherit your codebehind class from a proper umbraco masterpage class and put the full name of the class into "Inherits=...".

    Despite all that I still think that it's not a good practice to add any code to umbraco templates, since it mixes some program logic with layout. For my taste the far much righteous way is to solve such problems with user controls, umbraco macros and so on.

    To show the different parts of a content page depending on the current user you can make use of the standard asp:LoginView control, placing different parts of your page into its sections: http://msdn.microsoft.com/en-us/library/cc295194.aspx

  • Darren 12 posts 32 karma points
    Jan 20, 2012 @ 13:13
    Darren
    0

    Thanks for the very quick response :-).  In terms of the codefile stuff, do you mean create a new template in Umbraco and give it the same name as the master page but with .cs at the end of it? then place the button click code in this code file? Could you give an example?

     

     

     

  • Rodion Novoselov 694 posts 859 karma points
    Jan 20, 2012 @ 13:42
    Rodion Novoselov
    0

    Not such easy, but not much difficult. Actually you can name the .cs file with any name you like. The main point is to declare it as a CodeFile for the .master file you need.

    First of all you create the .cs file with the code like that (name it for instance Foo.cs):

    // file Foo.cs

    namespace SomeNamespace {

    public class MyAwesomeMasterPage: System.Web.UI.MasterPage {

    }

    }

    Than change the very first line of your template .master file this way:

    <%@ Master Language="C#" MasterPageFile="~/masterpages/YourTemplate.master" AutoEventWireup="true"
    CodeFile="Foo.cs" Inherits="SomeNamespace.MyAwesomeMasterPage" %>

    Than you can add to the MyAwesomeMasterPage class any usual stuff that can be added to a masterpage codebehind class (event handlers and so on).

    Another way can be to compile your MyAwesomeMasterPage class into some dll beforehand and put the one into the bin folder. In this case the @Master instruction should read like:

    <%@ Master Language="C#" MasterPageFile="~/masterpages/HtmlPage.master" AutoEventWireup="true"
        Inherits="SomeNamespace.MyAwesomeMasterPage, YourDllWithoutExtension" %> 

    (i.e. CodeFile should be omitted).

     

    Anyway, as I said before I would much rather avoid these things and do that in more standard umbraco-way (i.e. templates should be used for layout-purposes only - no program logic should be linked to them).

     

  • Darren 12 posts 32 karma points
    Jan 20, 2012 @ 14:23
    Darren
    0

    thanks you again for the reply, So, the more standard Umbraco way would be to create a user control / create macro for it, Yes?  I don't mind doing that at all, but If so, how do I link this user control with the content pages?, as the user control would need to set some sort of global level property (to know user has entered correct username and password, and logged in), and then the content pages would need to show and hide content appropriately...your ideas would be very much appreciated...

    ..essentially all I want to do is show the content of part of a page (which is some documents that the user can click on to download), when they have logged in as a specific user and not show this part when the user has not logged in....

     

  • Rodion Novoselov 694 posts 859 karma points
    Jan 20, 2012 @ 15:12
    Rodion Novoselov
    0

    You don't even need a custom control to display content depending on a user's authentication status (if I'm right that it's what you want). As I wrote before, there's a standard asp.net control for this purpose (the LoginView control). All you need is to place it to your template page like this:

    <asp:LoginView runat="server">
      <AnonymousTemplate>
          <%-- here goes the content for non-authenticated users --%>
      </AnonymousTemplate>
      <LoggedInTemplate>
           <%-- logged-in user content --%>
      </LoggedInTemplate>
    </asp:LoginView> 

    That's it - the control will do all the magic for you. You can place anything you want inside <***Template> tags, including text, another asp.net controls, page field controls, macro controls, etc.

  • Darren 12 posts 32 karma points
    Jan 23, 2012 @ 14:01
    Darren
    0

    Great, that works a treat!, Thank you for your help with this.  I must say I'm very impressed with the quick response with my question(s), thank you again.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft