How to dynamically hide a widget on a page?

Posted by Community Admin on 04-Aug-2018 18:26

How to dynamically hide a widget on a page?

All Replies

Posted by Community Admin on 02-Jun-2014 00:00

I am attempting to selectively hide a widget on a page based upon a query string parameter in codebehind.  Most of the site is public facing with only a few items that will be intended for users within the company's domain.  The widgets that have special class property of PrivateUseOnly or PublicUseOnly would be retrieved and the visibility of the widget would be set accordingly.

 I have set the page's Title & Properties "Code behind type (for ASP.NET developers)" to "SitefinityWebApp.TestCodeBehind, SitefinityWebApp".

The following code is being executed and I am locating the correct widgets.  However, I do not appear to be having any affect on the rendering of the widget.

using System;
using System.Linq;
using System.Web.UI;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Web;
 
namespace SitefinityWebApp
    public class TestCodeBehind : Page
    
        protected void Page_PreRender(object sender, EventArgs e)
        
            var pageManager = PageManager.GetManager();
            var page = pageManager.GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Page;
 
            var q = this.Page.Request.QueryString["PortalBarrier"];
            var isInside = !q.IsNullOrEmpty() && string.Compare(q, "inside", StringComparison.OrdinalIgnoreCase) == 0;
 
            foreach (PageControl control in page.Controls)
            
                var cssClass = control.Properties.FirstOrDefault(x => x.Name == "CssClass");
                if (cssClass == null)
                
                    continue;
                
 
                var isPrivateUseOnlySpecified = cssClass.Value.Contains("PrivateUseOnly");
                var isPublicUseOnlySpecified = cssClass.Value.Contains("PublicUseOnly");
                if (isPrivateUseOnlySpecified)
                
                    SetVisibility(control, isInside);
                
 
                if (isPublicUseOnlySpecified)
                
                    SetVisibility(control, !isInside);
                
            
        
 
        private void SetVisibility(PageControl control, bool visible)
        
            var property = control.Properties.FirstOrDefault(x => x.Name == "Visible");
            if (property == null)
            
                property = new ControlProperty
                
                    Name = "Visible",
                    Value = visible ? "true" : "false",
                    LastModified = DateTime.Now,
                    Id = Guid.NewGuid(),
                    Control = control
                ;
                control.Properties.Add(property);
            
            else
            
                property.Value = visible ? "true" : "false";
            
        
    

I have tried this on the Page_Load and the Page_Init events with no better success.

Posted by Community Admin on 04-Jun-2014 00:00

Hold on there!  it should be way easier than that...

public static IEnumerable<T> GetControlsOfType<T>(Control root) where T : Control
    var t = root as T;
 
    if (t != null)
        yield return t;
    
 
    var container = root;
 
    if (container != null)
        foreach (Control c in container.Controls)
            foreach (var i in GetControlsOfType<T>(c))
                yield return i;
            
        
    

On page_load, something like:

 

var privateControls = GetControlsOfType<Control>(Page).Where(o => o.CssClass == "PrivateUseOnly").ToList();
var publicControls = GetControlsOfType<Control>(Page).Where(o => o.CssClass == "PublicUseOnly").ToList();
 
//Hide stuff based on logged in or not?
var loggedIn = ClaimsManager.GetCurrentIdentity().IsAuthenticated;
 
privateControls.ForEach(o => o.Visible = loggedIn);
publicControls.ForEach(o => o.Visible = !loggedIn);

 

Posted by Community Admin on 05-Jun-2014 00:00

Hi,

Did you try by setting the visibility inside the widget itself? For instance, you can create the widget using Sitefinity Thunder and check for the property in the InitializeControls method as shown in this video. The method should look like this:

protected override void InitializeControls(GenericContainer container)
        
            ...
 
            var q = Page.Request.QueryString["PortalBarrier"];
            var isInside = !q.IsNullOrEmpty() && string.Compare(q, "inside", StringComparison.OrdinalIgnoreCase) == 0;
 
            if (isInside && this.CssClass.Contains("PrivateUseOnly")) this.Visible = false;
        

More information about creating widgets using Thunder below:

http://www.sitefinity.com/documentation/gettingstarted/create-a-widget-with-sitefinity-thunder


Kind Regards,
Junior Dominguez
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 05-Jun-2014 00:00

Yeah!  or.... simply use View Permissions on the widget editing by Role/Authenticated/Anonymous or whatever?

Posted by Community Admin on 06-Jun-2014 00:00

Thanks everyone for your assistance.

Anything that required authentication was not going to work for me as there is none.  Only whether you are inside the portal or outside.  The customer does not want any user authentication in this instance.

Now first off, thanks to Stephen2, who sent me in the direction I needed.  In the Page_PreRender event, I now search the Page for the controls desired.  A couple of modifications were necessary for my solution. Since the controls can be of any type, I look for controls that have a CssClass property and that its value contains either "PrivateUseOnly" or "PublicUseOnly".  The other change made was to not continue to look into it's child controls.  I only want the highest level controls that contain the setting.  Then, for these controls I set the visibility to the correct state.  Works like a charm.

Second, thanks to Junior Dominguez, who sent me in a direction to solve the similar condition that relates to content.  Not only can just the widgets be selective in what they show, but so can the data (Blogs, documents, etc.).  A new custom field has been added to the content storage (called "IsPrivate") the the users submitting content would check if they do not want the public to see it.  Since I needed to check a system setting, modifying the FilterExpression via the widgets advanced options was not going to get it.  By created a new class that inherits from base class, such as "public class MyMasterPostsView : Telerik.Sitefinity.Modules.Blogs.Web.UI.Public.MasterPostsView", I override the InitilizeControls and modify the FilterExpression that is depending upon both the IsPrivate field of the data and the Request.QueryString setting.  I do have to modify the Sitefinity Settings for ContentView | Controls | Blog PostsFrontend | Views | MasterBlogPostsFrontend and change the ViewType text box to contain my assembly and not Teleriks (such as "SitefinityWebApp.MyMasterPostsView, SitefintiyWebApp")  This, too, is working like a charm.  I just have to create similar classes for the ones that need the same functionality.

Posted by Community Admin on 07-Jun-2014 00:00

Smart stuff mate - very nice!!     

Posted by Community Admin on 03-Jul-2014 00:00

Hi,
 I have one content block and one custom module widget on page to
show news. I have to show only one of them based on,which type of news
admin wants to show. If admin wants the news from content block then
widget should be hidden and vice versa. Can anyone help me on this, how I
can take admin's choice of which news section to choose and how to hide
other news section based on his choice.
For better understanding of my question screenshot of page is attached here.

Thanks,
Tejal Satre

Posted by Community Admin on 07-Jul-2014 00:00

Hello Tejal,

Could you please elaborate on how you will persist and get the admin's choice value? One of the ways will be to hide the widget using a Page Codebehind adding it in the page Title and Properties, the other one, to alter the widget themselves, similar to what Junior suggested. Is a client-side solution with javascript appropriate in this scenario for you?

Regards,
Nikola Zagorchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 08-Jul-2014 00:00

Hi Nikola,

Thanks for reply. I have created custom field which will contain list of radio buttons. This field is editable from Page's Title and Properties option. And on page load we are checking the selected value of radio button and according to that we are hiding and showing the widget using javascript. Issue is resolved.

Thanks,

Tejal Satre

Posted by Community Admin on 10-Jul-2014 00:00

Hi Tejal,

I am glad you have resolved the issue. You can share your approach with the community if you would like to.

Regards,
Nikola Zagorchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed