TreeView Disappears on PostBack

2 posts, 0 answers
  1. Scott
    Scott avatar
    3 posts
    Member since:
    Apr 2013

    Posted 25 Aug 2014 Link to this post

    I have a RadTreeView on a form that is being populated by code with the node.add method described in your documentation. (http://www.telerik.com/help/aspnet-ajax/treeview-load-on-demand-client.html)  It works great for loading the items, but when you click one or do anything that causes a PostBack, all the items disappear.

    APSX
    <%@ Page Title="" Language="C#"  MasterPageFile="~/Content/themes/Level_One.Master" AutoEventWireup="true" CodeBehind="RequestManager.aspx.cs" Inherits="WebApplication.Forms.RequestManager" %>
     
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ MasterType VirtualPath="~/Content/themes/Level_One.Master" %>
     
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <%: System.Web.Optimization.Styles.Render("~/Content/css") %>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="main_placeholder" runat="server">
        <telerik:RadCodeBlock ID="cb1" runat="server">
            <script type="text/javascript">
                function mngRequestStarted(ajaxManager, eventArgs) {
                    // alert(eventArgs.get_eventTargetElement())
                    var newArr = new Array();
                    for (var name in ajaxManager) {
                        newArr.push(name + "<-->" + ajaxManager[name])
     
                    }
                }
     
            </script>
        </telerik:RadCodeBlock>
     
        <telerik:RadScriptManager ID="main_radscriptmanager" runat="server" EnableViewState="true"></telerik:RadScriptManager>
        <telerik:RadAjaxLoadingPanel ID="main_loading" runat="server" Skin="Metro" EnableViewState="true"></telerik:RadAjaxLoadingPanel>
     
            <telerik:RadAjaxManager ID="main_ajaxmanager" runat="server" EnableAJAX="true" EnableViewState="true" ClientEvents-OnRequestStart="mngRequestStarted">
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="tvRequestList">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="tvRequestList" LoadingPanelID="main_loading" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
     
                    <telerik:AjaxSetting AjaxControlID="main_windowmanager">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="main_windowmanager" LoadingPanelID="main_loading" />
                            <telerik:AjaxUpdatedControl ControlID="wndw_permissions" LoadingPanelID="main_loading" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                    <telerik:AjaxSetting AjaxControlID="btnSave" EventName="Click">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="btnSave" UpdatePanelRenderMode="Inline" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                    <telerik:AjaxSetting AjaxControlID="btnCancel"></telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
     
        <asp:Button ID="cmdTest" runat="server" OnClick="cmdTest_Click" />
        <telerik:RadSplitter ID="RadSplitter1" runat="server" Height="700px" Width="100%" EnableViewState="false">
              <telerik:RadPane ID="Pane1" runat="server" Height="100%" Width="20%">
                   <telerik:RadTreeView ID="tvRequestList" runat="server"
                       Skin="Metro" PersistLoadOnDemandNodes="true"
                       OnNodeExpand="tvRequestList_NodeExpand"
                       OnNodeClick="tvRequestList_NodeClick">
     
                   </telerik:RadTreeView>
              </telerik:RadPane>
              <telerik:RadSplitBar ID="RadSplitbar1" runat="server" CollapseMode="Forward">
              </telerik:RadSplitBar>
              <telerik:RadPane ID="Pane2" runat="server">
                   Pane2 - 100px
              </telerik:RadPane>
      <%--        <telerik:RadSplitBar ID="Radsplitbar2" runat="server" CollapseMode="Forward">
              </telerik:RadSplitBar>
              <telerik:RadPane ID="Pane3" runat="server" Height="150">
                   Pane3 - 150px
              </telerik:RadPane>--%>
         </telerik:RadSplitter>
        <telerik:RadWindowManager runat="server" ID="RadWindowManager1" EnableViewState="true"></telerik:RadWindowManager>
    </asp:Content>

    Code Behind
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Jabil.Core;
    using DataEntities.Application.Context;
    using DataEntities.Application.Entities;
    using Telerik.Web.UI;
    using System.Collections;
    using System.Collections.Generic;
     
    namespace WebApplication.Forms
    {
        public partial class RequestManager : FrameworkPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                this.Master.PageTitle = "Request Manager";
     
                if (!IsPostBack)
                {
                    LoadRootNodes(tvRequestList);
                }
            }
     
            private void LoadRootNodes(RadTreeView tv)
            {
                using (RequestsDataContext rdc = new RequestsDataContext(string.Empty))
                {
                    List<RequestManagerTreeList> tl = rdc.GetRequestManagerTreeViewList(1, 0, false).ToList<RequestManagerTreeList>();
                    foreach (RequestManagerTreeList tlItem in tl)
                    {
                        RadTreeNode tn = new RadTreeNode();
                        tn.Text = tlItem.ItemName;
                        tn.Value = ("L1" + tlItem.ItemId.ToString());
                        tn.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                        tv.Nodes.Add(tn);
                    }
                }
            }
     
            protected void tvRequestList_NodeExpand(object sender, RadTreeNodeEventArgs e)
            {
                try
                {
                    using (RequestsDataContext rdc = new RequestsDataContext(string.Empty))
                    {
                        string itemValue = e.Node.Value;
     
                        if (itemValue.Substring(0, 1) == "L")
                        {
                            int level = Convert.ToInt32(itemValue.Substring(1, 1)) + 1;
                            int itemId = Convert.ToInt32(itemValue.Substring(2, itemValue.Length - 2).ToString());
                            string levelTag = "L" + level.ToString();
                            List<RequestManagerTreeList> tl = rdc.GetRequestManagerTreeViewList(level, itemId, false).ToList<RequestManagerTreeList>();
                            foreach (RequestManagerTreeList tlItem in tl)
                            {
                                RadTreeNode tn = new RadTreeNode();
                                tn.Text = tlItem.ItemName;
                                tn.Value = levelTag + tlItem.ItemId.ToString();
                                if (level < 3)
                                {
                                    tn.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                                }
                                e.Node.Nodes.Add(tn);
     
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                    show_error_message(ex.Message);
                }
            }
     
            protected void tvRequestList_NodeClick(object sender, RadTreeNodeEventArgs e)
            {
                show_error_message("Test");
            }
     
            private void show_error_message(string error_message)
            {
     
                string error = error_message.Replace("'", "");
     
     
                main_ajaxmanager.ResponseScripts.Add(string.Format("window.radalert(\"" + error + "\")"));
     
            }
     
            protected void cmdTest_Click(object sender, EventArgs e)
            {
                show_error_message("Test");
            }
        }
    }

  2. Scott
    Scott avatar
    3 posts
    Member since:
    Apr 2013

    Posted 25 Aug 2014 in reply to Scott Link to this post

    After doing a lot of playing with this, I discovered there was an option being set on the RadSplitter that said EnableViewState="false".  After removing that, it worked fine.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top