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

RadTreeview Expand & collapse not happening

4 Answers 140 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Bikash
Top achievements
Rank 1
Bikash asked on 26 Nov 2013, 09:57 AM
Hi,

I have below  javascript  function in code-behind file for expand & collapse node:

private void SetActions()

{

String sString =

@"

function SaveTreeState(action)

{

var tree = $find('" + RadTreeView1.ClientID + @"');

if(tree == null) { return; }

var nodes = tree.get_allNodes();

if(action == 'expand')

{

for(var cnt=0; cnt<nodes.length; cnt++)

{

nodes[cnt].expand();

}

}

if(action == 'collapse')

{

for(var cnt=0; cnt<nodes.length; cnt++)

{

nodes[cnt].collapse();

}

}

var expandedNodes = '';

for(var cnt=0; cnt<nodes.length; cnt++)

{

if(nodes[cnt].get_expanded())

{

expandedNodes += nodes[cnt].get_value() + '|';

}

}

SaveCookie('mpnotetreestate',expandedNodes);

}

function SaveCookie(name,value,days) {

if (days) {

if (days>730) {

days=730;

}

var date = new Date();

date.setTime(date.getTime()+(days*24*60*60*1000));

var expires = '; expires='+date.toGMTString();

}

else var expires = '';

document.cookie = name+'='+value+expires+'; path=/';

}

";

ScriptManager.RegisterClientScriptBlock(this, typeof(ViewNotes), "SaveTreeStateBlock", sString, true);

ViewToolBar1.btnExpandAll.Attributes.Add("onclick", "try{SaveTreeState('expand');}catch(err){return;}return false;");

ViewToolBar1.btnCollapseAll.Attributes.Add("onclick", "try{SaveTreeState('collapse');}catch(err){return}return false;");

}



In UI I am using the below:
<cc2:MPTreeView BorderStyle="None" OnNodeDataBound="RadTreeView1_NodeDataBound" OnClientNodeCollapsed="SaveTreeState"

OnClientNodeExpanded="SaveTreeState" ShowLineImages="false" ID="RadTreeView1" runat="server">

<CollapseAnimation Duration="100" Type="None" />

<ExpandAnimation Duration="100" Type="None" />



I am not able to collapse the treeview control on button click nor able to expand ? Could u please suggest.

Thanks
Bikash

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Nov 2013, 10:59 AM
Hi Bikash,

Please have a look into the following code snippet to expand/collapse RadTreeView on RadButton click.

ASPX:
<telerik:RadTreeView ID="RadTreeView1" runat="server" DataSourceID="SqlDataSource1"
    DataFieldID="id" DataFieldParentID="parentid" DataTextField="text">
</telerik:RadTreeView>
<telerik:RadButton ID="RadButton1" runat="server" Text="Expand/Collapse TreeView"
    AutoPostBack="false" OnClientClicked="OnClientClicked1">
</telerik:RadButton>

JavaScript:
<script type="text/javascript">
    function OnClientClicked1(sender, args) {
        var treeview = $find("<%=RadTreeView1.ClientID %>");
        var length = treeview.get_allNodes().length;
        for (var i = 0; i < length; i++) {
            treeview.get_allNodes()[i].toggle();
        }
    }
</script>

Thanks,
Shinu.
0
Bikash
Top achievements
Rank 1
answered on 27 Nov 2013, 06:52 AM
Hi Shinu,

Can you please look into the code sample I have provided what's missing in that.
I can't use ur sample code as we r using .ascx page & it has other dependency as per our project standards.

Thanks
Bikash
0
Nencho
Telerik team
answered on 02 Dec 2013, 07:34 AM
Hello Bikash,

I have performed several tests, regarding the provided code snippet and it seems that it's working properly at my end. Here is a video, demonstrating the behavior at my end. Please make sure that you are correctly assigning the functions for expanding and collapsing to the btnExpandAll/btnCollapseAll of the ToolBar that you are using.

Regards,
Nencho
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Bikash
Top achievements
Rank 1
answered on 02 Dec 2013, 11:22 AM
Hi Nencho,

It's working with the piece of the code.  The code was correct & I was not getting any error while doing client side script debugging.But due to some external dependency it was not working earlier .

However thanks for your help & support.

Thanks
Bikash
Tags
TreeView
Asked by
Bikash
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bikash
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or