Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 10th, 2006, 05:07 AM
Registered User
 
Join Date: Jul 2006
Location: ram, ram, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tree View Control host in SPS

Hi ,

Step by Step Approach..

1. Created Web Application with Tree View control functionality is given down

2. Hosted using Page viewer web part..

 This is Ram Naresh , I worked on Tree View control and placed in Share Point site. I observed some of the things which will will worthy to share.

I created Tree View control . our Requirement forces one node at a time should open .. if one node expand all other silblings and child nodes should be close this will prevent vertical scorlling .

I written Java script for that...

<script language="javascript">

    function TreeDefaultStyel()
    {
       var treeHanld = GetHandler();
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
      treeHanld.oncontentready();
    }

 // Intercepts the collapsed event on the client.
    //
    function NodeExpand(obj)
    {
    var treeHanld = GetHandler();
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
       CollapseOtherNodes(obj,treeHanld);//selected node
    }


    function IndexChanged(obj)
    {
       var treeHanld = GetHandler();
       var treeNode;
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
      treeNode = treeHanld.getTreeNode(obj); //current Node
      if(null == treeNode || undefined == treeNode)
      {
        return;
      }//tree node
       var ID=treeNode.getAttribute('id') //Tree ID.
       if(null != treeNode.getAttribute('nodeData') && null ==treeNode.getAttribute('NavigateUrl') )
       {
         if (treeNode.getAttribute('nodeData').indexOf("&") > -1)
          {
    strNavigationURL=treeNode.getAttribute('nodeData') +"&ID="+ID;
          }
          else
          {
           strNavigationURL=treeNode.getAttribute('nodeData') +"?ID="+ID;
          }
          treeHanld.getTreeNode(obj).setAttribute('NavigateU rl',strNavigationURL);
      }
  }


 // Gets a handle to the TreeView's selected node.
    //
    function CollapseOtherNodes(obj,treeHanld)
    {
      var tree = treeHanld;
      var treeNode,treeParent, treeChild,ownChilds;
      var nodeIndex,curnodeIndex;
      var blnRoot =false;

      if ( null == tree || undefined == tree )
        return null;


  treeNode = tree.getTreeNode(obj); //current Node

  if ( null != treeNode && undefined != treeNode )
  {
           ownChilds= treeNode.getChildren();//get its own childs.
     treeParent = treeNode.getParent();//its parent..
     if ( null != treeParent && undefined != treeParent )
     {
       nodeIndex= treeParent.getNodeIndex()//Parent Node index
     }
     curnodeIndex= treeNode.getNodeIndex()//current Node index
   }

   if ( null != nodeIndex && undefined != nodeIndex )
   {
     treeParent = tree.getTreeNode(nodeIndex); //Get Parent..
   }
   else
   {
    treeParent = tree;
    blnRoot=true;
   }
   if(blnRoot)//Root Node expand
    {
    var ID = document.getElementById('hdnStatus');
    var NodeID;
    if(ID != null )
    {
      tree = tree.getChildren();
      tree= tree[0].getChildren();

      for(j=0; j< tree.length ; j++)
      {

        NodeID = tree[j].getAttribute('id');
        if(NodeID == ID.value)
        {
        tree[j].setAttribute('expanded',true);
        }
        else
        {
           tree[j].setAttribute('expanded',false);
        }
      }


    }
    }
    else //Other Node Expansion
    {
    if ( null != treeParent && undefined != treeParent )
    {
     treeChild = treeParent.getChildren(); //siblings..
    }

    for(i=0; i< treeChild.length ; i++)
    {
     nodeIndex= treeChild[i].getNodeIndex()
     if(nodeIndex != curnodeIndex)
     {
     tree.getTreeNode(nodeIndex).setAttribute('expanded ',false);
     }
     else//own childs..
     {
     if(ownChilds != null || ownChilds != nothing)
     {
      for(j=0; j< ownChilds.length ; j++)
       {
        nodeIndex= ownChilds[j].getNodeIndex()
        tree.getTreeNode(nodeIndex).setAttribute('expanded ',false);
       }

     }

     }
    }//for end
       }
     }


 // Gets a handle to the TreeView.
    function GetHandler()
    {
      var tree;
      var treeName = 'EPTreeView';

      // Get a handle to the TreeView.
      tree = document.getElementById( treeName );

      if ( null == tree || undefined == tree )
        return null;

      return tree;
    }



  </script>



And need to add Attributes in Cs File.

private void AddHandlers()

{

string clickHandler="";

// Create and wire up the javascript event handlers.

//

clickHandler = "IndexChanged(this.clickedNodeIndex);";

this.EPTreeView.Attributes.Add( "onselectedindexchange",clickHandler );


clickHandler = "NodeExpand(this.clickedNodeIndex);";

this.EPTreeView.Attributes.Add( "onexpand", clickHandler );



}



We hosted this web application in share point by using Page Viewer web part.

on the first instance we faced lot of problem when we are redirecting the page is displaying in page viewer webpart this is because of Targer property of node is set to =_self we replaced it to _parent the url is changed it is redirected to another site.



The code is like this....

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient ;

using System.Configuration ;

using Microsoft.Web.UI.WebControls ;



namespace EPDashboardTreeView

{

public class TreeView : System.Web.UI.Page

{

private SqlConnection sqlcon;

protected System.Web.UI.HtmlControls.HtmlInputHidden hdnStatus;

protected Microsoft.Web.UI.WebControls.TreeView EPTreeView;

private int intExpandLevel =5;//value from Query String.


private void Page_Load(object sender, System.EventArgs e)

{


try

{

if(!Page.IsPostBack)

{

DataSet dsNodeList = null;

Hashtable ht ;

AddHandlers();//To add handlers

getTreeList(ref dsNodeList);


if(Request.QueryString["ID"] != null)

{

intExpandLevel=int.Parse(Request.QueryString ["ID"]);

}

else

{

intExpandLevel=int.Parse(hdnStatus.Value);

}


if (intExpandLevel > 0)

{

ht = new Hashtable();

AllParents(ref ht,dsNodeList,intExpandLevel);//Get all Parent Nodes

doExpand(EPTreeView.Nodes,ht,intExpandLevel); //Expand Parent of its all

}

else//Node ID =0

{

EPTreeView.ExpandLevel =1; //initial state

}


}

}

catch

{

}


}



#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

/// <summary>

///

/// </summary>

/// <param name="dsNodeList"></param>

/// <returns></returns>

private int getTreeList(ref System.Data.DataSet dsNodeList)

{

TreeNode rootNode=null;

TreeNode tn,tc;


int intParentID=0;

int intNodeID =0;

int intLevelAt=1;

int isPopUP;

string strNodeName,strURL,strImgURL="treeimages/arrow.gif";


try

{

dsNodeList = ExecuteSP("GetTreeNodes",null);


if(dsNodeList !=null)

{

for(int index=0; index<dsNodeList.Tables[0].Rows.Count;index++)

{

intNodeID= int.Parse(dsNodeList.Tables[0].Rows[index]["NodeID"].ToString());

strNodeName= dsNodeList.Tables[0].Rows[index]["NodeName"].ToString();

strURL = dsNodeList.Tables[0].Rows[index]["URL"].ToString();

intParentID= int.Parse(dsNodeList.Tables[0].Rows[index]["ParentID"].ToString());

intLevelAt = int.Parse(dsNodeList.Tables[0].Rows[index]["LevelAt"].ToString());

isPopUP = int.Parse(dsNodeList.Tables[0].Rows[index]["IsPopUP"].ToString());

if(intNodeID==0) //Root Node

{

rootNode = new TreeNode();

rootNode.ID = intNodeID.ToString();//ID

rootNode.Text = "" + strNodeName + "";

rootNode.NavigateUrl =strURL;

rootNode.NodeData = strURL;

rootNode.Target ="_top";

EPTreeView.Nodes.Add(rootNode);


}

else

{

tn = new TreeNode();

doRecursion(EPTreeView.Nodes ,intParentID.ToString(),ref tn);


if(tn !=null)

{

bool blnIsparent = false;

blnIsparent= ChildElement(dsNodeList,intNodeID);

tc= new TreeNode();

tc.ID = intNodeID.ToString();

tc.Text = "" + strNodeName + "";

tc.NodeData =strURL;

tc.Target = "_top";

//tc.NavigateUrl =strURL;

tn.Nodes.AddAt(intLevelAt-1,tc);

if(isPopUP==1)//Pop up

{

tc.NavigateUrl =strURL;

tn.Nodes[intLevelAt-1].Target ="_blank";

}

if(blnIsparent)

{

tn.Nodes[intLevelAt-1].ImageUrl =strImgURL;

}

}

}


}

}

}

catch (Exception ex)

{

//Excepiton goes here

Response.Write(ex.Message + "<br/> Level at Database is wrong Node ID : "+intNodeID);

return -1;

}

return 0;

}

bool ChildElement(DataSet dsNodeList,int NodeID)

{

DataRow []dr ;

dr= dsNodeList.Tables[0].Select("ParentID= "+ NodeID);

if(dr == null || dr.Length ==0)

{

return true;

}

return false;

}

/// <summary>

///

/// </summary>

/// <param name="t"></param>

/// <param name="strParentID"></param>

/// <param name="Tfin"></param>

void doRecursion(Microsoft.Web.UI.WebControls.TreeNodeC ollection t,string strParentID,ref TreeNode Tfin)

{

bool bln=false;

try

{

foreach(Microsoft.Web.UI.WebControls.TreeNode tn in t)

{

if(tn.ID.Trim() == strParentID.Trim())

{

Tfin = tn;

bln=true;

return;

}


doRecursion(tn.Nodes,strParentID,ref Tfin );

}

}

catch

{

bln =false;

}


if(bln) return;

}

/// <summary>

///

/// </summary>

/// <param name="spname"></param>

/// <param name="parmArry"></param>

/// <returns></returns>

private System.Data.DataSet ExecuteSP(string spname, string[][] parmArry)

{

System.Data.DataSet dsResult = new System.Data.DataSet();

string strParameterNam;

string strParameterVal;

try

{

sqlcon = new SqlConnection (ConfigurationSettings.AppSettings["ConnectionString"]);

if ( sqlcon.State == System.Data.ConnectionState.Closed)

{

sqlcon.Open();

}


SqlCommand cmd = sqlcon.CreateCommand();

cmd.CommandText=spname;

cmd.CommandType = System.Data.CommandType.StoredProcedure ;


if (parmArry !=null)

{

for(int index =0; index < parmArry.Length ; index ++)

{

strParameterNam = parmArry[index][0];

strParameterVal= parmArry[index][1];

if (strParameterNam != null && strParameterVal != null)

{


}


}

}

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dsResult);

}

catch

{

dsResult =null;

//Excepiton goes here

}

finally

{

sqlcon.Close();


}

return dsResult;

}


/// <summary>

/// Get all of itsParents

/// </summary>

/// <param name="ht"></param>

/// <param name="dsList"></param>

/// <param name="intNodeID"></param>

void AllParents(ref Hashtable ht,DataSet dsList, int intNodeID)

{

DataRow []dr =null;

object objKey="";

try

{

ht.Add(intNodeID,intNodeID);

while(true)

{

dr=dsList.Tables[0].Select("NodeID ="+ intNodeID);

objKey= (object)dr[0]["ParentID"];

ht.Add(objKey,objKey);

intNodeID= (int)objKey;

if(intNodeID == 0 ) //root Node

{

break;

}

}

}

catch

{

}


}



/// <summary>

/// Expand all nodes to specified Node

/// </summary>

void doExpand(Microsoft.Web.UI.WebControls.TreeNodeColl ection t,Hashtable ht,int NodeID)//,string intExpandParentID)

{

try

{

foreach(Microsoft.Web.UI.WebControls.TreeNode tn in t)

{

if( (int.Parse(tn.ID) <= NodeID)&&(ht.ContainsKey(int.Parse(tn.ID ))))

{

tn.Expanded =true;

}

else

{

tn.Expanded =false;

}


doExpand(tn.Nodes,ht,NodeID);

}

}

catch

{

}

}


private void AddHandlers()

{

string clickHandler="";

// Create and wire up the javascript event handlers.

//

clickHandler = "IndexChanged(this.clickedNodeIndex);";

this.EPTreeView.Attributes.Add( "onselectedindexchange",clickHandler );


clickHandler = "NodeExpand(this.clickedNodeIndex);";

this.EPTreeView.Attributes.Add( "onexpand", clickHandler );



}




}

}

and Aspx page is ..

 <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@ Register TagPrefix="iewc1" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@ Page language="c#" Codebehind="TreeView.aspx.cs" AutoEventWireup="false" Inherits="EPDashboardTreeView.TreeView" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>TreeView</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body bgColor="#4a7bde" MS_POSITIONING="GridLayout" onload="TreeDefaultStyel()">
  <script language="javascript">

    function TreeDefaultStyel()
    {
       var treeHanld = GetHandler();
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
      treeHanld.oncontentready();
    }

 // Intercepts the collapsed event on the client.
    //
    function NodeExpand(obj)
    {
    var treeHanld = GetHandler();
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
       CollapseOtherNodes(obj,treeHanld);//selected node
    }


    function IndexChanged(obj)
    {
       var treeHanld = GetHandler();
       var treeNode;
    if(null == treeHanld || undefined == treeHanld)
      {
        return;
      }//handler
      treeNode = treeHanld.getTreeNode(obj); //current Node
      if(null == treeNode || undefined == treeNode)
      {
        return;
      }//tree node
       var ID=treeNode.getAttribute('id') //Tree ID.
       if(null != treeNode.getAttribute('nodeData') && null ==treeNode.getAttribute('NavigateUrl') )
       {
         if (treeNode.getAttribute('nodeData').indexOf("&") > -1)
          {
    strNavigationURL=treeNode.getAttribute('nodeData') +"&ID="+ID;
          }
          else
          {
           strNavigationURL=treeNode.getAttribute('nodeData') +"?ID="+ID;
          }
          treeHanld.getTreeNode(obj).setAttribute('NavigateU rl',strNavigationURL);
      }
  }


 // Gets a handle to the TreeView's selected node.
    //
    function CollapseOtherNodes(obj,treeHanld)
    {
      var tree = treeHanld;
      var treeNode,treeParent, treeChild,ownChilds;
      var nodeIndex,curnodeIndex;
      var blnRoot =false;

      if ( null == tree || undefined == tree )
        return null;


  treeNode = tree.getTreeNode(obj); //current Node

  if ( null != treeNode && undefined != treeNode )
  {
           ownChilds= treeNode.getChildren();//get its own childs.
     treeParent = treeNode.getParent();//its parent..
     if ( null != treeParent && undefined != treeParent )
     {
       nodeIndex= treeParent.getNodeIndex()//Parent Node index
     }
     curnodeIndex= treeNode.getNodeIndex()//current Node index
   }

   if ( null != nodeIndex && undefined != nodeIndex )
   {
     treeParent = tree.getTreeNode(nodeIndex); //Get Parent..
   }
   else
   {
    treeParent = tree;
    blnRoot=true;
   }
   if(blnRoot)//Root Node expand
    {
    var ID = document.getElementById('hdnStatus');
    var NodeID;
    if(ID != null )
    {
      tree = tree.getChildren();
      tree= tree[0].getChildren();

      for(j=0; j< tree.length ; j++)
      {

        NodeID = tree[j].getAttribute('id');
        if(NodeID == ID.value)
        {
        tree[j].setAttribute('expanded',true);
        }
        else
        {
           tree[j].setAttribute('expanded',false);
        }
      }


    }
    }
    else //Other Node Expansion
    {
    if ( null != treeParent && undefined != treeParent )
    {
     treeChild = treeParent.getChildren(); //siblings..
    }

    for(i=0; i< treeChild.length ; i++)
    {
     nodeIndex= treeChild[i].getNodeIndex()
     if(nodeIndex != curnodeIndex)
     {
     tree.getTreeNode(nodeIndex).setAttribute('expanded ',false);
     }
     else//own childs..
     {
     if(ownChilds != null || ownChilds != nothing)
     {
      for(j=0; j< ownChilds.length ; j++)
       {
        nodeIndex= ownChilds[j].getNodeIndex()
        tree.getTreeNode(nodeIndex).setAttribute('expanded ',false);
       }

     }

     }
    }//for end
       }
     }


 // Gets a handle to the TreeView.
    function GetHandler()
    {
      var tree;
      var treeName = 'EPTreeView';

      // Get a handle to the TreeView.
      tree = document.getElementById( treeName );

      if ( null == tree || undefined == tree )
        return null;

      return tree;
    }



  </script>
  <form id="TreeView" method="post" runat="server">
   &nbsp;&nbsp; <INPUT id="hdnStatus" type="hidden" size="8" value="5" runat="server" NAME="hdnStatus"
    style="Z-INDEX: 101; LEFT: 304px; WIDTH: 1px; POSITION: absolute; TOP: 8px; HEIGHT: 1px">
   <iewc:TreeView id="EPTreeView" style="Z-INDEX: 102; LEFT: 0px; POSITION: absolute; TOP: 0px" runat="server"
    ShowLines="False" SystemImagesPath="treeimages/"></iewc:TreeView>
  </form>
 </body>
</HTML>





Data base..

'use BMPReportsDB



if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTreeNodes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTreeNodes]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

--Get All the Tree Nodes.

CREATE PROCEDURE GetTreeNodes

AS

SELECT * FROM bmpTreeView ORDER BY parentID , levelat ASC
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO




GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[BMPTreeView]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[BMPTreeView]
GO

CREATE TABLE [dbo].[BMPTreeView] (
 [NodeID] [int] NOT NULL ,
 [NodeName] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
 [ParentID] [int] NOT NULL ,
 [LevelAt] [int] NOT NULL ,
 [URL] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [IsPopup] [int] NULL
) ON [PRIMARY]


GO

INSERT INTO BMPTreeView (NodeID, NodeName, ParentID, LevelAt, URL, IsPopup)
 VALUES (0,'EPDashboard',-1,0,'',0)
GO

Node id is unique id .. Parent ID tells this node belongs to which parent . and Level at will tell .. child no in that parent there may be n no of childern for that parent need to specify order.. isPopup will tell u need to open other window or not ( 1/0)


-- Regards

Ram Naresh Yadav Talluri.

Any quaries contact me at ramnaresh_t@yahoo.com







Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tree View Control Java script Expand /CollpaseAll Ram Naresh General .NET 0 July 10th, 2006 05:08 AM
Tree view control g_vamsi_krish ASP.NET 1.0 and 1.1 Basics 2 April 4th, 2006 11:02 PM
Microsoft Tree view control humour General .NET 0 November 27th, 2004 09:40 AM
Tree View Control pran ASP.NET 1.0 and 1.1 Professional 1 September 2nd, 2004 11:41 AM



All times are GMT -4. The time now is 07:52 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.