Access radiolist in RadTreeView

3 posts, 0 answers
  1. Sudhanva
    Sudhanva avatar
    27 posts
    Member since:
    Apr 2009

    Posted 12 Sep 2010 Link to this post

    Hi,

    My Telerik Version is 2009.1.402.35.

    I want to access RadioButtonList in RadTreeNode. How to do this?

    Here is the sample code in which i'm not able to access the radio control.

    public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadTreeView1.DataSource = Getdata();
                RadTreeView1.DataTextField = "FirstName";
                RadTreeView1.DataValueField = "ID";
                RadTreeView1.DataBind();
      
                foreach (RadTreeNode node in RadTreeView1.Nodes)
                {
                    //Create First Level Node
                    RadTreeNode node1 = new RadTreeNode("Test1", node.Value);
      
                    //Create Second Level Node and make it checkable false
                    RadTreeNode node2 = new RadTreeNode();
                    node2.Checkable = false;
      
                    RadioButtonList radio = new RadioButtonList(); radio.Items.Add(new ListItem("P", "1")); radio.Items.Add(new ListItem("S", "0"));
                    //Add RadioButton to node2
                    node2.Controls.Add(radio);
      
                    //Add Node2 to Node1
                    node1.Nodes.Add(node2);
      
                    //Add Node1 to main Node
                    node.Nodes.Add(node1);
                }
            }
        }
      
        public DataTable Getdata()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));
            dt.Rows.Add("1", "Sudhanva", "G");        
            dt.Rows.Add("3", "Raman", "C V");
            dt.Rows.Add("4", "Hrithik", "Roshan");
            return dt;
      
        }
      
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (RadTreeNode node in RadTreeView1.Nodes)
                {
                    foreach (RadTreeNode node1 in node.Nodes)
                    {
                        foreach (RadTreeNode node2 in node1.Nodes)
                        {
                            RadioButtonList radio1 = (RadioButtonList)node2.Controls[0];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //RadioButtonList radio12 = (RadioButtonList)RadTreeView1.Nodes[0].Nodes[0].Controls[0];
      
        }
        
    }

    .ascx code:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
        Inherits="WebUserControl" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <br />
    <br />
    <telerik:RadTreeView ID="RadTreeView1" runat="server" CheckBoxes="true" >
    </telerik:RadTreeView>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />

    .aspx code:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
        CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>
      
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
      
      
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
         
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
      
     <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="WebUserControl1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="WebUserControl1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                  <telerik:AjaxSetting AjaxControlID="Button1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="WebUserControl1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
          
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
    </asp:Content>

     

    Please help.

    Thanks in advance,
    Sudhanva

  2. Sudhanva
    Sudhanva avatar
    27 posts
    Member since:
    Apr 2009

    Posted 12 Sep 2010 Link to this post

    Attached is the screen shot of the UI, FYI.
  3. Nikolay Tsenkov
    Admin
    Nikolay Tsenkov avatar
    734 posts

    Posted 14 Sep 2010 Link to this post

    Hello Sudhanva,

    The problem here is the following:
    - The nodes are templated in a wrong way!

    Currently your nodes are being templated once (on initial load of the page), while they should be templated on every postback too.
    The way to implement this would be:
    1. move the implementation of Page_Load handler in Page_Init time;
    2. add ParentId to your data source and create the nodes only through databinding;
    3. in Page_Init handler rebind the tree only if it's not postback;
    4. in the dataSource add a value column (nullable), and assign value which should indicate if the node should be templated or not;
    5. in Page_Init handler iterate through the nodes of the treeView (GetAllNodes would do the job to get all nodes in array and iterate through them) checking the value of the nodes, and by some criteria determining whether this node should be templated or not, and if it should be, then add the control in it's controls collection;
    6. when you try to get reference to the control inside the controls collection of a node, please, do it this way:
    RadioButtonList radio1 = (RadioButtonList)node2.FindControl("RadioButtonList1");

    Also after you get this to work, you probably will have to use a better style of templating - implementing ITemplate interface that we provide. Here is a nice little article on that: http://www.telerik.com/help/aspnet-ajax/tree_templatesruntime.html
    I recommend  that you use this way of templating (it even shows some of the above mentioned measures that you need to take, like moving templateing in Page_Init time and so on).

    Additional thought:
    - If you upgrade your controls, you can make use of the newly introduced event TemplateNeeded, not just over RadTreeView, but also over RadPanelBar, RadListBox, RadToolBar and on many others of the rest of the RadControls package.

    Please, try to implement the suggested and if you experience some problems during this process, don't hesitate to write to us again!

    Hope this is helpful for you!


    Regards,
    Nikolay Tsenkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top