Results 1 to 11 of 11

Thread: ASP Net - C# - TreeView

  1. #1
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts

    ASP Net - C# - TreeView

    heya peeps

    i am very new to the whole c#/.Net stuff and tbh am struggling with it.

    i am doing the whole normal thing of trying to run before 1 can walk but its stressed me out a bit tonight so thought if someone can point me in the right direction it may help.

    what i would really like to do is create a class which can be used in both ASP web apps and Windows based Apps. 1st is this possible to have a totally shared class in this manner?

    What i need it to do is something like creating a tree struct ie
    Code:
    -My House
      -ground floor
         -kitchen
         -sitting room
      -1st floor
         - bedroom
    etc u get the picture. All these locations would be stored in a SQL2000 DB which relate to each other using parent/child IDs against each record.

    From reading about the treeview structure u need to populate this via XML which can be done from the SQL 2000 db using "for xml" in the sql statement.

    Where i seem to be having problems though is simply getting my WebForm aspx file accepting any input from my .cs class file where i have the methods for creating said tree.

    I think i am going wrong from the aspx page side of things as when using VS 2005 beta i see the program enter the class file but the tree struct isnt displayed on when the page loads.

    so i guess what i am asking is

    how do i create an object of a class which i have manually created and display it on an .aspx page?


    cheers
    Jon

  2. #2
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Don't take this as gospel, I'm still a beginner at this myself, but you should be able to create a class and use it anywhere you like.
    First in a file called "MyTreeClass.cs" or "MyTreeClass.vbs" or whatever...
    Code:
    namespace  LaughingJon.MyClasses
    {
       public class JonsTreeClass
       {
          //all you class stuff here
       }
    }
    Then in "JonsWebForm.aspx.cs" or "JonsWebForm.aspx.vbs" or just in the <code> section of "JonsWebForm.aspx" itself.
    Code:
    using LaughingJon.MyClasses;
    
    public class JonsWebForm
    {
    	public LaughingJon.MyClasses.JonsTreeClass anInstanceOfMyTreeClass;
    
    	private void Page_Load(object sender, System.EventArgs e)
    	{
    	   if (! IsPostBack)
    	   {
    			// do stuff with anInstanceOfMyTreeClass
    	   }
    	}
    }
    And you can do pretty much the same in your winforms files. Just make sure all the files are included in your project (or referenced or whatever)
    They told me I was gullible ... and I believed them.

  3. #3
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts
    ooooooh cunning..

    thanks for the reply i ditched trying to do this and have continued to read my books lol..

    however that all makes sense to me.. i think something to try tonight

    thanks again for taking time out to reply.

    l8rs
    Jon

  4. #4
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts
    right well according to the debug stuff in VS it is definately going through the constructors and doing the right things as expected. however, how do i tell the .aspx file that i want to display this tree as a structure on the front page lol.

    its mad, i can see it run through creating the objects, i can see the objects using the debug tools but no matter what i try it seems i am totally unable to make the tree display in IE ROFL!

  5. #5
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Hi Jon,

    Sorry for not getting back sooner, and that this answer's not going to help tuppence. I'm afraid I don't know the first thing about tree-views.

    Guessing again, do you have the correct gubbins in the aspx page? Something like
    Code:
    <asp:treeview id="anInstanceOfMyTreeClass" runat="server" visible="true"></asp:treeview>
    Apart from that I'm afraid you're on your own.

    Keep trying though - Andrew.
    They told me I was gullible ... and I believed them.

  6. #6
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    Is this using the MS IE Web Controls pack?

    I've just spent 2 weeks developing a document management system at work and used the MS TreeView control extensively. It's a bit quirky, and a bit buggy, but it's really slick when you set it up right

    I'd steer clear of using SQL Server to generate XML as there's no real need. You can bind any dataset to the tree.

    What I did was to extract the dataset from the DB on the first initialisation, build the tree from it, and then modify the treenodes as required for each further action the user takes.

    One of the biggest drawbacks to this version of the control (which has been fixed in ASP.NET 2.0) is that every action the user makes with the tree is recorded in the viewstate. This can lead to very very large page sizes quite quickly. However, there's absolutely no reason why you can't override the Page class methods that store and retrieve the viewstate, which is what I did. You can store the viewstate in the hidden form field (default), or a file, or a session variable, or a text file, or a DB... you get the picture

    Make sure you read the documentation that's available and try and get the samples working first. Then move on to creating your own clesses and what not. If you have any specific questions please ask
    Simon


  7. #7
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts
    cheers Andrew i will double check that include line just to make sure i havent made an error there tonight. (i deleted all the code out of frustration hehehee.. so will have to start over again from scratch) this may be a good thing though.

    Shad i am using Visual Studio 2005 beta which i believe is using the ASP.NET 2.0. I got a simple tree structure added to a page and added nodes to this manually. All these were visible on the .aspx page. That is when i went onto creating my own "tree" structure in a seperate .cs file.

    This is then where i had all the problems as i am totally stuck trying to get this .cs file included in the .aspx front page. The .cs file wasnt doint anything flash i was just manually creating a treeview structure again and manually adding nodes to this again. When debugging i saw all this happening but its the displaying it on the seperate .aspx page is where i am still struggling.

    cheers
    Jon

  8. #8
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    Can you paste the first line of the .aspx file please
    Simon


  9. #9
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts
    well i have tried again and yet again failed.

    i can only think i am approaching this from totally the wrong angle so if u can point me in right direction that would be grand.

    The .aspx file is as so.
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreePage.aspx.cs" Inherits="TreePage"  %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Label ID="stname" runat="server" Text="Label"></asp:Label>
        <asp:TreeView ID="fronttree" runat="server"></asp:TreeView>
        </div>
        </form>
    </body>
    </html>
    with the code behind .cs file like this.
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class TreePage : System.Web.UI.Page
    {    
       private ACustomTree st;
        protected void Page_Load(object sender, EventArgs e)
        {
            st = new ACustomTree();
            stname.Text = st.Name + "node count = " + st.Nodes.Count;
            fronttree = st;
            
             //i have also just tried
             //fronttree = new ACustomTree();
             //didnt work either.
    
        }
    
        public class ACustomTree : TreeView
        {
            private String name;
    
            public String Name { 
                get
                { 
                    return name; 
                }
                set
                { 
                    name = value; 
                }
            }
    
            public ACustomTree() 
            {
                name = "test tree name";
                addNodes();
            }
    
            private void addNodes()
            {
                TreeNode ROOT = new TreeNode("ROOT");
                this.Nodes.Add(ROOT);
                ROOT.ChildNodes.Add(new TreeNode("123"));
                ROOT.ChildNodes.Add(new TreeNode("321"));
                ROOT.ChildNodes.Add(new TreeNode("222"));
                ROOT.ChildNodes.Add(new TreeNode("333"));
    
            }
        }
    }
    all that i get displayed is the string saying

    "test tree namenode count = 1"

    and no TreeView whatsoever.

    hopefully u r like right now and can see a noob trap i have fallen into but alas i cant see it lol

    obviously the problem is the assigning back to the fronttree object and it not being drawn properly, but alas i am not sure how to fix that.

    pootles off to keep fiddling and hopfully falls over the solution lol.

    cheers
    Jon
    Last edited by LaughingJon; 06-10-2005 at 09:19 PM.

  10. #10
    Member
    Join Date
    Aug 2005
    Posts
    100
    Thanks
    2
    Thanked
    0 times in 0 posts
    ok finally managed to achieve this simple task via the only way i know. several cans of carling and a good long sit and ponder.

    for those who are interested in getting custom web objects onto your forms it kinda goes like this

    front asp page
    Code:
    <form id="formname" runat="server">
        <div>
            <asp:Panel runat="server" ID="WebPanel"></asp:Panel>
        </div>
        </form>
    code behind page
    Code:
    protected aTree LJTree;
        protected void Page_Load(object sender, EventArgs e)
        {
            LJTree = new aTree();
            
            this.WebPanel.Controls.Add(this.LJTree);
    
        }
    
        public class aTree : TreeView
        {
            public aTree()
            {
                TreeNode tmp = new TreeNode("hexus");
                TreeNode tmp2 = new TreeNode("hexus1");
                TreeNode tmp3 = new TreeNode("hexus2");
                TreeNode tmp4 = new TreeNode("hexus3");
    
                this.Nodes.Add(new TreeNode("TOP"));
                tmp3.ChildNodes.Add(tmp4);
                tmp2.ChildNodes.Add(tmp3);
                tmp.ChildNodes.Add(tmp2);
                this.Nodes.Add(tmp);            
            }      
        }
    key points to see are:

    create a panel on the front page.. name it what ever (in this case WebPanel)
    then in your code do
    this.WebPanel.Controls.Add(this.LJTree);
    where in this case LJTree is the treeview object.

    hope it helps anyone else stuck like me

    l8rs
    Jon
    Last edited by LaughingJon; 12-10-2005 at 12:02 AM.

  11. #11
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    Hmm

    This is ASP.NET 2 isn't? I've just been thinking... "how is this working with a non-compiled code behind file and no control definitions"

    I don't really see why you have to assign a new control instance to the control on the page. Can you not build upon the control that already exists?

    If you had a repeater control for example, you wouldn't create a new repeater control in code and bind to it, then assign it to the control on the page - you'd just bind to the control on the page

    I dunno... maybe it's all changed (for the better no doubt) in .NET 2, but I'm sure you'd have some dire problems with relating postback data to a control if you assigned a new instance to it like that in .NET 1.

    Glad you got it working anyway!
    Simon


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 18-03-2005, 05:03 PM
  2. Solutions to net security fears
    By Steve in forum HEXUS News
    Replies: 4
    Last Post: 02-03-2005, 06:59 PM
  3. Two pints of bitter and some net access please
    By Flibb in forum PC Hardware and Components
    Replies: 9
    Last Post: 22-11-2004, 03:13 PM
  4. ASP Help required
    By TomWilko in forum Software
    Replies: 1
    Last Post: 27-10-2004, 02:36 PM
  5. Learning ASP
    By TomWilko in forum Software
    Replies: 9
    Last Post: 02-10-2004, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •