Readonly node

3 posts, 0 answers
  1. Jakob
    Jakob avatar
    2 posts
    Member since:
    Aug 2010

    Posted 05 Jan 2011 Link to this post

    Hi Telerik

    I'm trying to get at node i a treeview to behave as normal, but the user must not be able to change the nodes checked state.
    Is it posible to make a node "ReadOnly" but behave as normal?

    I've made a small test project to show that i'm looking for.

    Hope you understand my problem.

    The Html
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadTreeView.Default" %>
      
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <telerik:RadTreeView ID="RadTreeView1" runat="server" CheckBoxes="True" 
                TriStateCheckBoxes="True" DataFieldID="id" DataFieldParentID="Parrentid" 
                onnodedatabound="RadTreeView1_NodeDataBound" CheckChildNodes="true">
                  
            </telerik:RadTreeView>
        </div>
        </form>
    </body>
    </html>


    Code behind
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
      
    namespace RadTreeView
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                RadTreeView1.CheckBoxes = true;
                RadTreeView1.DataSource = TreeDataItem.GetTreeData();
                RadTreeView1.DataBind();
            }
      
            protected void RadTreeView1_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
            {
                TreeDataItem _node = e.Node.DataItem as TreeDataItem;
      
                e.Node.Text = _node.Text;
                //e.Node.Checkable = !_node.ReadOnly;
                e.Node.Checked = _node.Selected;
      
                if (_node.ReadOnly)
                {
                    e.Node.ImageUrl = "CheckedCheckbox.jpg";
                    e.Node.Checked = true;
                    e.Node.Enabled = false;
                    e.Node.Attributes.Add("style", "Color: #000000;");
                }
            }
        }
    }

    Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
      
    namespace RadTreeView
    {
        public class TreeDataItem
        {
            private string _text;
      
            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }
      
            private int _id;
      
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }
      
            private int _parrentId;
      
            public int ParrentId
            {
                get { return _parrentId; }
                set { _parrentId = value; }
            }
      
            private bool _readOnly;
      
            public bool ReadOnly
            {
                get { return _readOnly; }
                set { _readOnly = value; }
            }
      
            private bool _selected;
      
            public bool Selected
            {
                get { return _selected; }
                set { _selected = value; }
            }
      
      
            public TreeDataItem (int id, int parrentId, string text, bool Readonly, bool selected)
            {
                _id = id;
                _parrentId = parrentId;
                _text = text;
                _readOnly = Readonly;
                _selected = selected;
            }
      
            public static List<TreeDataItem> GetTreeData()
            {
                List<TreeDataItem> data = new List<TreeDataItem>();
      
                data.Add(new TreeDataItem(1, 0, "Beer", false, false));
                data.Add(new TreeDataItem(2, 1, "Carlsberg", false, true));
                data.Add(new TreeDataItem(3, 1, "Heineken", false, false));
                data.Add(new TreeDataItem(4, 0, "Food", false, false));
                data.Add(new TreeDataItem(5, 4, "Burger", false, true));
                data.Add(new TreeDataItem(6, 4, "Pizza", false, false));
                data.Add(new TreeDataItem(7, 4, "Sandwitch", true, true));
                data.Add(new TreeDataItem(8, 4, "Spagetti", false, false));
                return data;
            }
        }
    }
  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1268 posts

    Posted 07 Jan 2011 Link to this post

    Hi Jakob,

    Thank you for contacting us.

    One possible solution to make a node which does not change its checked or unchecked state is to subscribe on OnClientNodeChecked and OnClientNodeChecking events and persist the previous state of the node whenever one of these events is fired. Though this will cause a little inconvenience using tri-state property of the treeview , because the parent node won't be able to be set in unchecked state since one of its child node will always be checked.
    I'm sending an example project demonstrating what you might use as an approach for your needs.
    I hope this will help.

    All the best,
    Dimitar Terziev
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. Jakob
    Jakob avatar
    2 posts
    Member since:
    Aug 2010

    Posted 30 Mar 2011 Link to this post

    thanx
Back to Top