This is a migrated thread and some comments may be shown as answers.

RadPanelItem parent tree

1 Answer 78 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Phil DeVeau
Top achievements
Rank 1
Phil DeVeau asked on 21 Jul 2009, 01:58 AM
I'm using some user controls in a web app with some custom security designed around different product offerings.  Each product offering is defined as a user control which inherits from a custom <MyUserControl> : UserControlBase : UserControl.  As each user control is loaded I'm calling a recursive method to check the parent tree to find the PanelBarItem and disable it if the user control is disabled.

using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Text;  
 
/// <summary>  
/// Summary description for UserControlBase  
/// </summary>  
public class UserControlBase : UserControl  
{  
    public UserControlBase() : base()  
    {  
        Log("UserBase Constructor");  
        base.Load += new EventHandler(UserControlBase_Load);  
    }  
 
    void UserControlBase_Load(object sender, EventArgs e)  
    {  
        Log("UserBase Load");  
        bool renderable = false// || false;  
        SetParentPanel(Parent, renderable);  
    }  
 
    public void SetParentPanel(Control parent, bool enabled)  
    {  
        string key = "TestEnabled";  
        Log(enabled.ToString());  
 
        if (parent == null)  
            return;  
 
        Log(parent.GetType().ToString());  
 
        if (parent is Telerik.Web.UI.RadPanelItem)  
        {  
            Telerik.Web.UI.RadPanelItem panel = parent as Telerik.Web.UI.RadPanelItem;  
            if (string.IsNullOrEmpty(panel.Attributes[key]))  
                panel.Attributes.Add(key, enabled.ToString());  
 
            if (enabled)  
                panel.Attributes[key] = "true";  
 
            if (Convert.ToBoolean(panel.Attributes[key]))  
            {  
                Log("Enabling");  
                panel.Enabled = true;  
                return;  
            }  
            else 
            {  
                Log("Disabling");  
                panel.Enabled = false;  
                return;  
            }  
        }  
 
        SetParentPanel(parent.Parent, enabled);  
    }  
 
    public static void Log(string log)  
    {  
        const int LOGSIZE = 150;  
        const int KBYTESIZE = 1024;  
        const string FILELOC = @"c:\logfiles\testlog.log";  
        using (System.IO.FileStream fs = new System.IO.FileStream(FILELOC, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))  
        {  
            System.IO.StreamReader reader = new System.IO.StreamReader(fs);  
            log += "\n" + reader.ReadToEnd();  
            int max = LOGSIZE * KBYTESIZE;  
            byte[] bytes = Encoding.UTF8.GetBytes(log);  
            if (bytes.Length < max)  
            {  
                fs.SetLength(0);  
                fs.Write(bytes, 0, bytes.Length);  
            }  
            else 
            {  
                fs.SetLength(0);  
                fs.Write(bytes, 0, max);  
            }  
            fs.Flush();  
            fs.Close();  
        }  
    }  
}  
 

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> 
<%@ Register Src="~/WebControl1.ascx" TagName="TestControl" TagPrefix="my" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">  
    <telerik:RadPanelBar ID="RadPanelBar1" runat="server" Skin="Default2006" Width="100%">  
        <Items> 
            <telerik:RadPanelItem runat="server" Text="Control1">  
                <Items> 
                    <telerik:RadPanelItem> 
                        <ItemTemplate> 
                            <my:TestControl runat="server" ID="TestControl1" /> 
                        </ItemTemplate> 
                    </telerik:RadPanelItem> 
                </Items> 
            </telerik:RadPanelItem> 
        </Items> 
        <CollapseAnimation Duration="100" Type="None" /> 
        <ExpandAnimation Duration="100" Type="None" /> 
    </telerik:RadPanelBar> 
</asp:Content> 
 


So far I haven't gotten this to work as defined in the above sample.  Any ideas on how to traverse up and get the telerik RadPanelItem?

1 Answer, 1 is accepted

Sort by
0
Phil DeVeau
Top achievements
Rank 1
answered on 21 Jul 2009, 03:16 PM
Figured it out.

public void SetParentPanel(Control parent, bool enabled)  
    {  
        string key = "ProductPanelEnabled";  
        if (parent == null)  
            return;  
 
        if (parent is Telerik.Web.UI.RadPanelItem)  
        {  
            using (Telerik.Web.UI.RadPanelItem panel = parent as Telerik.Web.UI.RadPanelItem)  
            {  
                if (!string.IsNullOrEmpty(panel.Text))  
                {  
 
                    if (string.IsNullOrEmpty(panel.Attributes[key]))  
                        panel.Attributes.Add(key, enabled.ToString());  
 
                    if (enabled)  
                        panel.Attributes[key] = "true";  
 
                    if (Convert.ToBoolean(panel.Attributes[key]))  
                    {  
                        panel.Enabled = true;  
                        return;  
                    }  
                    else 
                    {  
                        panel.Enabled = false;  
                        return;  
                    }  
                }  
            }  
        }  
 
        SetParentPanel(parent.Parent, enabled);  
    } 
Tags
PanelBar
Asked by
Phil DeVeau
Top achievements
Rank 1
Answers by
Phil DeVeau
Top achievements
Rank 1
Share this question
or