Link to home
Start Free TrialLog in
Avatar of csetzkorn
csetzkorn

asked on

treeview initialise checkboxes

Hi,

I have no problem to bind some XML data on a treeview. I am just wondering whether the checkboxes could also be initialised from the xml file when I bind it (e.g., each node in the xml file has an attribute 'checked'). Of course, I could somehow? initialise the checkboxes programmatically.

Any input would be very much appreciated. Many thanks.

Chris
ASKER CERTIFIED SOLUTION
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of csetzkorn
csetzkorn

ASKER

Hi,

Thanks I will have a look at these links. I though that I could somehow initialise the status of the CheckBoxes via the TreeNodeBinding as I can do with other properties of the TreeView.

TreeNodeBinding binding = new TreeNodeBinding();
binding.TextField = "title";
binding.Value = "node_uid";

Thanks.
Ohh csetz,

You can try out the Event TreeNodeDataBound (invoked after the treeNode is created).

protected void tvNavigation_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
        //check your XML file and do some if else here
        e.Node.Checked = true / false;
    }

The e.Node will return a TreeNode that raises the event (so you can get Text, Value of course) by e.Node.Text or e.Node.Value (you can use it to check your XML content binded to)

Hope this help

Jinny
BTW:

This is the structure of the XML I receive from the database:

<node selection ="0" title="Kingdom" node_uid = "81">

selection indicates whether the checkbox is checked (0) or not (1).

Of course, I can loop through the XML and the tree and set the checkboxes accordingly - but I thought there is a better way to do this  (e.g. use TreeNodeBinding)

Thanks,

C
Try my new post ^^
We gotta see after binding how your treeView would looks like (what the text and value of each TreeNode)

But in anyway that matter, you can try out that TreeNodeDataBound ( I mentioned in the second post), because this event is occured everytime after every single TreeNode is binded.

jinn_hnnl,

I currently attempt to set the OnTreeNodeDataBound of the treeview to try what you suggested. However, I cannot set it (the TreeViews are created in the code behind file and added to a Panel).

Error      1      'System.Web.UI.WebControls.TreeView.OnTreeNodeDataBound(System.Web.UI.WebControls.TreeNodeEventArgs)' is inaccessible due to its protection level      

Any suggestions to overcome this?

Thanks.

Chris
cset ...

when you load and add the treeView Programmatically in every postback Page. You can do this:

protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)    {      
        }
       myTreeView.TreeNodePopulate += new TreeNodeEventHandler(myTreeView_TreeNodePopulate);
    }

private void myTreeView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{.....
}


IF your treeView is not something global within the webform. You still can do this righat after you create a treeView:

void MyMethodToCreateTReeView()
{
     TreeView myTreeView = new TreeView();
     myTreeView.myTreeView.TreeNodePopulate += new TreeNodeEventHandler(myTreeView_TreeNodePopulate);

  //... then bindData here for tree node
}

private void myTreeView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{.....
}
Chris,

That's the common way to define event for Web Control, when it is programmatically created ^^

Hope it would help you

Jinny
ok, I have done it. I created a recursive method which receives the treeview object and the xml data. the checkbox's state is set according to the xml data.

private void iterateNode(TreeNode node, XmlNode root)
    {
        // deal with node
        string node_uid = node.Value;
        XmlNode temp_node = root.SelectSingleNode("//node[@node_uid ='" + node_uid + "' ]");

        // "//node[@node_uid ='" + node_uid + "' ]"      and selection = '1' does not work ???!!!

        if ((temp_node != null) && (temp_node.Attributes[ 0 ].Value.ToString().Equals("1") == true))
        {
            //Response.Write( temp_node.Attributes[ 0 ].Value.ToString() + ", " + temp_node.Attributes[ 1 ].Value.ToString() + ", " + temp_node.Attributes[ 2 ].Value.ToString() + "<br>" );
            node.Checked = true;
        }

        // deal with children
        for (int c = 0; c < node.ChildNodes.Count; c++)
        {
            iterateNode(node.ChildNodes[c], root);
        }
    }

I will accept the first answer.
sorry i meant the current node not the treeview object
Good job Chirs,

I thought you bind it just by setting the datasource connect to the XML file.  I also have this problem before when I tried to programatically add the node to the tree (using recursive function).

There is an option in which you use OnTreeNodePopulate(); (only add new treenode upon client demand, in another word: when client expand the parent node). This way it can be abit more complicated if you work with XML, but if you work with database... it's perfect.

Jinny ^^