TreeNodeExpandMode.ServerSideCallBack problem.

1 posts, 0 answers
  1. Arindam Tewary
    Arindam Tewary avatar
    1 posts
    Member since:
    Aug 2009

    Posted 10 Aug 2009 Link to this post

    Hi,
    I have used "TreeNodeExpandMode.ServerSideCallBack" mode in ExpandMode of each nodes being added as child note but these chile nodes are not populated while expanding a root node.
    How can I fix this problem.
    However if I use "TreeNodeExpandMode.ServerSide" as Expand mode in my chile nodes, it works perfectly.

    Following is my code to fill TreeView nodes
    ------------------------------------------------------------------------------------------
    Code behind file code
    -------------------------------------------------------------------------------------------

    using

     

    System;

     

    using

     

    System.Collections;

     

    using

     

    System.Configuration;

     

    using

     

    System.Data;

     

    using

     

    System.Linq;

     

    using

     

    System.Web;

     

    using

     

    System.Web.Security;

     

    using

     

    System.Web.UI;

     

    using

     

    System.Web.UI.HtmlControls;

     

    using

     

    System.Web.UI.WebControls;

     

    using

     

    System.Web.UI.WebControls.WebParts;

     

    using

     

    System.Xml.Linq;

     

    using

     

    System.Data.SqlClient;

     

    using

     

    Telerik.Web.UI;

     

    namespace

     

    MasterWeb35

     

    {

     

     

    public partial class WebFormDB : System.Web.UI.Page

     

    {

     

    protected void Page_Load(object sender, EventArgs e)

     

    {

     

    RadTreeNode rootNode = null;

     

     

    DataTable dTAllData = null;

     

     

     

    try

     

    {

     

    if (!Page.IsPostBack)

     

    {

     

    int iLevel = 1;

     

     

    int iParentNodeID = 0;

     

    dTAllData = GetInitialData(iLevel, iParentNodeID);

     

    /*

     

    oNodeMenu = new RadContextMenu();

    oNodeMenu.Items.Add(new RadMenuItem("Add"));

    oNodeMenu.Items.Add(new RadMenuItem("Edit"));

    rootNode.Controls.Add(oNodeMenu);

    */

     

    /*RadTreeNode rootNode = new RadTreeNode("Root Item");

     

    rootNode.Value = "1";

    rootNode.Expanded = true;

    rootNode.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;

    RadTreeView1.Nodes.Add(rootNode);*/

     

    /*RadTreeView1.DataSource = dTAllData;

     

    RadTreeView1.DataTextField = "AILevel1Name";

    RadTreeView1.DataValueField = "AILevel1ID";

    RadTreeView1.DataBind();*/

     

    for (int i = 0; i < dTAllData.Rows.Count; i++)

     

    {

    rootNode =

    new RadTreeNode(dTAllData.Rows[i]["NodeLevel"].ToString());

     

    rootNode.Value = dTAllData.Rows[i][

    "IDValue"].ToString();

     

    rootNode.Expanded =

    true;

     

    rootNode.ExpandMode =

    TreeNodeExpandMode.ServerSideCallBack;

     

    RadTreeView1.Nodes.Add(rootNode);

    }

    }

    }

     

    catch(Exception ex)

     

    {

    Response.Write(ex.Message +

    Environment.NewLine + ex.StackTrace);

     

    }

     

    finally

     

    {

    rootNode =

    null;

     

    }

    }

     

    public DataTable GetInitialData(int iLevel, int iparentNodeID)

     

    {

     

    SqlConnection oCon = null;

     

     

    SqlDataAdapter oSqlAdapter = null;

     

     

    DataSet dsData = null;

     

     

    DataTable dTAllData = null;

     

     

     

    try

     

    {

    oCon =

    new SqlConnection("myconnectionString")

     

    oCon.Open();

     

    if (oCon.State == ConnectionState.Open)

     

    {

    Response.Write(

    "Connected to DB");

     

    }

    dsData =

    new DataSet();

     

    oSqlAdapter =

    new SqlDataAdapter();

     

     

    SqlCommand oSqlCommand = new SqlCommand();

     

    oSqlCommand.Connection = oCon;

    oSqlAdapter.SelectCommand = oSqlCommand;

    oSqlAdapter.SelectCommand.CommandText =

    "usp_GetNodes";

     

    oSqlAdapter.SelectCommand.CommandType =

    CommandType.StoredProcedure;

     

    oSqlAdapter.SelectCommand.Parameters.AddWithValue(

    "@level", iLevel);

     

    oSqlAdapter.SelectCommand.Parameters.AddWithValue(

    "@NodeID",iparentNodeID);

     

    oSqlAdapter.Fill(dsData);

     

    if (dsData.Tables.Count > 0)

     

    {

    dTAllData = dsData.Tables[0];

    }

    }

     

    catch(Exception ex)

     

    {

    Response.Write(ex.Message+

    Environment.NewLine+ex.StackTrace);

     

    }

     

    finally

     

    {

    oCon =

    null;

     

    oSqlAdapter =

    null;

     

    dsData =

    null;

     

     

    }

     

    return dTAllData;

     

    }

     

    protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)

     

    {

     

    RadTreeNode rootNode = null;

     

     

    String sNodeIdVal = "";

     

     

    String[] sValues = null;

     

     

    int intParentNodeID = 0, intLevelID = 0;

     

     

    DataTable dTAllData = null;

     

     

    try

     

    {

    sNodeIdVal = e.Node.Value;

    sValues = sNodeIdVal.Split(

    ",".ToCharArray());

     

    intParentNodeID =

    Convert.ToInt32(sValues[0]);

     

    intLevelID =

    Convert.ToInt32(sValues[1]);

     

    intLevelID = intLevelID + 1;

     

    if (intLevelID <= 3)

     

    {

    dTAllData = GetInitialData(intLevelID, intParentNodeID);

     

    if (dTAllData.Rows.Count > 0)

     

    {

     

    for (int i = 0; i < dTAllData.Rows.Count; i++)

     

    {

    rootNode =

    new RadTreeNode(dTAllData.Rows[i]["NodeLevel"].ToString());

     

    rootNode.Value = dTAllData.Rows[i][

    "IDValue"].ToString();

     

    rootNode.Expanded =

    true;

     

    rootNode.ExpandMode =

    TreeNodeExpandMode.ServerSideCallBack;

     

    rootNode.Text = dTAllData.Rows[i][

    "NodeLevel"].ToString();

     

    e.Node.Nodes.Add(rootNode);

    }

    e.Node.Expanded =

    true;

     

    }

    }

    }

     

    catch (Exception ex)

     

    {

    Response.Write(ex.Message+

    Environment.NewLine+ex.StackTrace);

     

    }

     

    finally

     

    {

    dTAllData =

    null;

     

    sValues =

    null;

     

    }

    }

     

    private DataTable CreateDataTableForLevel1()

     

    {

     

    DataTable dTLevel1 = new DataTable();

     

    dTLevel1.Columns.Add(

    "ID");

     

    dTLevel1.Columns.Add(

    "Name");

     

    dTLevel1.Columns.Add(

    "ParentID");

     

     

    String[] Row1 = new String[] { "1", "GrandPa", "0" };

     

    dTLevel1.LoadDataRow(Row1,

    true);

     

     

    return dTLevel1;

     

    }

     

    private DataTable CreateDataTableForLevel2()

     

    {

     

    DataTable dTLevel2 = new DataTable();

     

    dTLevel2.Columns.Add(

    "ID");

     

    dTLevel2.Columns.Add(

    "Name");

     

    dTLevel2.Columns.Add(

    "Level1ID");

     

     

    String[] Row1 = new String[] { "4", "Father", "1" };

     

    dTLevel2.LoadDataRow(Row1,

    true);

     

     

    return dTLevel2;

     

    }

     

    private DataTable CreateDataTableForLevel3()

     

    {

     

    DataTable dTLevel3 = new DataTable();

     

    dTLevel3.Columns.Add(

    "ID");

     

    dTLevel3.Columns.Add(

    "Name");

     

    dTLevel3.Columns.Add(

    "Level2ID");

     

     

    String[] Row1 = new String[] { "1", "My brother", "4" };

     

     

    String[] Row2 = new String[] { "2", "Me", "4" };

     

    dTLevel3.LoadDataRow(Row1,

    true);

     

     

    return dTLevel3;

     

    }

    }

    }

    ------------------------------------------------------------------------------------------
    HTML Code
    -------------------------------------------------------------------------------------------

     

    <

     

    form id="form1" runat="server">

     

     

     

     

     

     

    <asp:ScriptManager ID="smScriptManager" runat="server"></asp:ScriptManager>

     

     

     

     

     

     

    <telerik:RadTreeView ID="RadTreeView1" runat="server" OnClientContextMenuShowing="onClientContextMenuShowing" Height="800px" Width="600px" wStyle="border: 1px solid #CBE7F5;"

     

     

     

     

     

     

    OnNodeExpand="RadTreeView1_NodeExpand">

     

     

     

     

     

     

    <ContextMenus>

     

     

     

     

     

     

    <telerik:RadTreeViewContextMenu ID="cMenu" runat="server" CausesValidation="true" EnableEmbeddedScripts="true">

     

     

     

     

     

     

    <Items>

     

     

     

     

     

     

    <telerik:RadMenuItem Text="Add" Value="1" />

     

     

     

     

     

     

    <telerik:RadMenuItem Text="Delete" Value="2"/>

     

     

    </Items>

     

     

     

     

     

     

    </telerik:RadTreeViewContextMenu>

     

     

     

     

     

     

    </ContextMenus>

     

     

     

     

     

     

    </telerik:RadTreeView>

     

     

    </form>

     

Back to Top