This is a migrated thread and some comments may be shown as answers.

RadTreeView Node ForeColor

2 Answers 323 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 2
Alex asked on 16 Jun 2009, 08:50 PM
Hello,
     I am trying to build a RadTreeView with drag and drop functionality into a RadGrid. I have succeeded in building the grid and the tree and making all of the functionality work well. Every time a node is dragged to the grid, the node in the tree is given the color red to show the the item is currently in the grid (I've gotten the color change to work as well). The tree is currently working to show available attributes for a given item. The problem is, when the page loads, if there are currently attributes within the grid, I cannot make them change colors to red. I am using the Page Service (Example) method to generate all of my information with the tree, and I am also using a web service to generate the information in the grid. Is there a way to change the color of the nodes to red if the node has been pre-selected (such as if the attributes are saved and someone leaves the page and comes back the items will be in the grid)?

     Also, if an item is taken out of the grid, I am also having a hard time getting the color back to the black forecolor. I realize it's along the same line.
C# 
 
     [WebMethod] 
    public static RadTreeNodeData[] LoadNodes(RadTreeNodeData node) // called each time a node is expanded 
    { 
        AttributeService.Attribute atts = new AttributeService.Attribute(); 
        List<RadTreeNodeData> result = new List<RadTreeNodeData>(); 
        DataSet ds = new DataSet(), ads = new DataSet(); 
        DataTable data = new DataTable(), attributeData = new DataTable(); 
        int WebP_Id = Int32.Parse(HttpContext.Current.Session["WebPID"].ToString()); 
        ArrayList RadNodeChanged = new ArrayList(); 
 
        ds = atts.GetChildrenByWebpId(WebP_Id, Convert.ToInt32(node.Value)); // gets the children for a specific item 
        data = ds.Tables[0]; 
 
        foreach (DataRow row in data.Rows) 
        { 
            RadTreeNodeData childNode = new RadTreeNodeData(); 
 
            childNode.Text = row["AttributeName"].ToString(); // adds the name and the text to "childNode" 
            childNode.Value = row["Attribute_Id"].ToString(); 
 
            if (atts.GetAllChildren(Convert.ToInt32(childNode.Value)).Tables[0].Rows.Count > 0) 
            { 
                childNode.ExpandMode = TreeNodeExpandMode.WebService; 
            } 
            //can code go here to make the color of the node red when the parent is expanded? 
 
            //attributeData = atts.GetAttributes(WebP_Id).Tables[0]; 
            //foreach (DataRow dr in attributeData.Rows) 
            //{ 
            //    if (String.Compare(dr["AttributeId"].ToString(), childNode.Value) == 0) 
            //    { 
            //        childNode.Attributes["ForeColor"] = System.Drawing.Color.Red; 
            //    } 
            //}   //doesn't work 
 
            result.Add(childNode); 
        } 
        return result.ToArray(); 
    } 
 
    private static void LoadRootAttributeNodes(RadTreeView treeView, TreeNodeExpandMode expandMode) 
    {  
        AttributeService.Attribute attser = new AttributeService.Attribute(); // function is called to set the origin nodes 
        int WebP_Id = Int32.Parse(HttpContext.Current.Session["WebPID"].ToString());   
        DataSet ds = new DataSet(); 
        ArrayList Cat = new ArrayList(); 
        DataTable dt = new DataTable(); 
 
        ds = attser.GetChildrenByWebpId(WebP_Id, 0); // zero being for the high level parent nodes 
        dt = ds.Tables[0]; 
 
        foreach (DataRow row in dt.Rows) 
        { 
            RadTreeNode node = new RadTreeNode(); 
            node.Text = row["AttributeName"].ToString(); 
            node.Value = row["Attribute_Id"].ToString(); 
            node.ExpandMode = expandMode; 
            treeView.Nodes.Add(node); 
        } 
         
    } 
 
    protected void AttributesTree_HandleDrop(object sender, RadTreeNodeDragDropEventArgs e) 
    { //drag and drop function, when an item is released from the tree, this function will be called 
        RadTreeNode sourceNode = e.SourceDragNode, destNode = e.DestDragNode;         
        RadTreeViewDropPosition dropPosition = e.DropPosition; 
        AttributeService.Attribute atts = new AttributeService.Attribute(); 
        ArrayList deletedCat = new ArrayList(); 
        DataSet ds = new DataSet(); 
        DataTable dt = (DataTable)Session["AttributesTable"]; 
        bool existsInList = false
 
     
        Session["TreeView"] = AttributeTree; 
 
        foreach (GridDataItem dataItem in CurrentAttributesList.Items) 
        {             
            if (dataItem.ForeColor == System.Drawing.Color.Red) 
             {                
                deletedCat.Add(dataItem["AttributeId"].Text); 
            } 
        } 
         
        foreach (RadTreeNode node in e.DraggedNodes) 
        { 
            existsInList = false
            foreach ( GridDataItem item in CurrentAttributesList.Items ) 
            { 
                if (item["AttributeId"].Text == node.Value) 
                { 
                    existsInList = true
                } 
            } 
            if (existsInList) 
            { 
                Master.AttributePublishErrorMessage = "Please use an attribute that is not in the list"// duplicate item 
            } 
            else 
            { 
                ds = atts.GetAttributeByAttributeID(Convert.ToInt32(node.Value)); 
                if (ds.Tables.Count > 0) 
                { 
                    foreach (DataRow dr in ds.Tables[0].Rows) 
                    { 
                        if (String.Compare(dr["Parent_ID"].ToString(), "0") == 0) 
                        { 
                            Master.AttributePublishErrorMessage = "Please use the lowest possible, non-parent attribute available."
                        } 
                        else 
                        { 
                            dt.Rows.Add(dr["Attribute_ID"], dr["AttributeName"], dr["AttributePath"]); 
                            foreach (RadTreeNode currentnode in AttributeTree.SelectedNodes) 
                            { 
                                currentnode.ForeColor = System.Drawing.Color.Red; 
                                RadTreeNodeChanged.Add(currentnode.Value);                                 
                            } 
                        } 
                    } 
                } 
            } 
        } 
 
        Session["AttributesTable"] = dt; 
        CurrentAttributesList.DataSource = dt; 
        CurrentAttributesList.DataBind(); 
 
        for (int i = 0; i < CurrentAttributesList.Items.Count; i++) 
        { 
            for (int j = 0; j < deletedCat.Count; j++) 
            { 
                if (CurrentAttributesList.Items[i]["AttributeId"].Text == deletedCat[j].ToString()) 
                { 
                    CurrentAttributesList.Items[i].Font.Strikeout = true
                    CurrentAttributesList.Items[i].ForeColor = System.Drawing.Color.Red; 
                } 
            } 
        } 
    } 
 
    public void CurrentAttributes(int webp_id) 
    { 
        AttributeService.Attribute atts = new AttributeService.Attribute(); 
        DataSet attributes = atts.GetAttributes(webp_id); 
        DataTable attributeData = attributes.Tables[0];         
        CurrentAttributesList.DataSource = attributeData; 
        CurrentAttributesList.DataBind(); 
        Session["AttributesTable"] = attributeData; 
    } 
    protected void CurrentAttributesList_Command(Object source, CommandEventArgs e) 
    { // the rad grid function when a command for save, delete, or refresh 
        int getWebP_Id = GetValidWebpId(SearchBox.Text), listIndex; 
        WebServiceProxy wsp = new WebServiceProxy(); 
        switch (e.CommandName) 
        { 
            case "SaveChanges"//when the command is save, the attribute will be saved to the database 
 
                AttributeService.Attribute atts = new AttributeService.Attribute(); 
                CategoryStatus attributeStatus, status; 
                currentAttributes = new ArrayList(); 
                DataSet CurrentAttributeList = atts.GetAttributes(getWebP_Id); 
                 
                //  load the current categories into a list, stores the status of each cat id 
                foreach (DataRow dataRowAttribute in CurrentAttributeList.Tables[0].Rows) 
                { 
                    attributeStatus.CatId = Convert.ToString(dataRowAttribute["AttributeId"]); 
                    attributeStatus.Status = 0; 
                    attributeStatus.Primary = false
 
                    currentAttributes.Add(attributeStatus); 
                } 
                // cycles through the rad grid to detect changes 
                foreach (GridDataItem dataItem in CurrentAttributesList.Items) 
                { 
                    string catid = dataItem["AttributeId"].Text; 
                    listIndex = FindInList(catid, currentAttributes); 
 
                    if (dataItem.ForeColor == System.Drawing.Color.Red) 
                    { 
                        updateListStatus(currentAttributes, ListStatus.Remove, listIndex); 
                    } 
 
                    if (listIndex < 0) //not in the list, needs to be added 
                    {                         
                        status.CatId = catid; 
                        status.Status = ListStatus.AddPrimary; 
                        status.Primary = false
                        currentAttributes.Add(status); 
                    } 
                    else 
                    { 
                        status = (CategoryStatus)currentAttributes[listIndex]; 
                    } 
                } 
                // stores the status of each cat id (add, remove, or no change) 
                foreach (CategoryStatus cs in currentAttributes) 
                { 
                    switch (cs.Status) 
                    { 
                        case ListStatus.NoChange: 
                            break
                        case ListStatus.AddPrimary: 
                            wsp.AddProductAttribute(getWebP_Id, Convert.ToInt32(cs.CatId), Request.ServerVariables["auth_user"], 0); 
                            break
                        case ListStatus.Remove: 
                            wsp.DeleteProductAttributes(getWebP_Id, Convert.ToInt32(cs.CatId)); 
                            break
                    } 
                } 
                CurrentAttributes(getWebP_Id); 
                 
 
            case "DeleteSelected"
                // using a for loop to loop through each item, then set the selected ones to be red and struck through 
                // need to call a rebind instead of binding to a table to keep font attributes 
                for (int i = 0; i < CurrentAttributesList.Items.Count; i++) 
                { 
                    if (CurrentAttributesList.Items[i].Selected) 
                    { 
                        CurrentAttributesList.Items[i].Font.Strikeout = true
                        CurrentAttributesList.Items[i].ForeColor = System.Drawing.Color.Red; 
                    } 
                } 
 
                CurrentAttributesList.Rebind();                 
                break
            case "RebindGrid"// refresh grid back to original state, before any changes were made after hitting the save button 
                getWebP_Id = GetValidWebpId(SearchBox.Text); 
                CurrentAttributes(getWebP_Id); 
 
                foreach (string id in RadTreeNodeChanged) 
                { 
                    ControlItem node = AttributeTree.FindChildByValue <ControlItem> (id); 
                    node.ForeColor = System.Drawing.Color.Black; //doesn't work 
                } 
                break
            default
                break
        } 
    } 


ASPX: 
 
 
<asp:Content ID="CurrentAttributes" runat="server" ContentPlaceHolderID="AttributesRightContent"
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"
        <contenttemplate> 
            <telerik:RadGrid ID="CurrentAttributesList" runat="server" Skin="Vista" AllowMultiRowSelection="true" Height="448px" AllowPaging="true" PageSize="20" AllowAutomaticDeletes="true"
                <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="AttributeID"
                      <CommandItemTemplate> 
                         <div style="padding: 5px 5px;"
                             <asp:LinkButton ID="btnSaveSelected" runat="server" CommandName="SaveChanges" OnCommand="CurrentAttributesList_Command"><img style="border:0px;vertical-align:middle;" alt="" src="Images/save.png" />&nbsp;Accept Changes</asp:LinkButton>&nbsp;&nbsp; 
                             <asp:LinkButton ID="btnDeleteSelected" OnClientClick="javascript:return confirm('Delete selected attributes?')" runat="server" CommandName="DeleteSelected" OnCommand="CurrentAttributesList_Command"><img style="border:0px;vertical-align:middle;" alt="" src="Images/Cancel.gif" />&nbsp;Delete selected customers</asp:LinkButton> &nbsp;&nbsp 
                             <asp:LinkButton ID="btnRefreshGrid" runat="server" CommandName="RebindGrid" OnCommand="CurrentAttributesList_Command"><img style="border:0px;vertical-align:middle;" alt="" src="Images/refresh.png" />&nbsp;Refresh attributes list to original</asp:LinkButton> 
                         </div> 
                      </CommandItemTemplate> 
                </MasterTableView> 
                <ClientSettings> 
                    <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" /> 
                </ClientSettings> 
            </telerik:RadGrid> 
        </contenttemplate> 
    </asp:UpdatePanel> 
</asp:Content> 
<asp:Content ID="AttributesTree" runat="server" ContentPlaceHolderID="AttributesLeftContent"
    <asp:UpdatePanel id="AttributeUpdatePanel" runat="server" UpdateMode="Conditional"
        <contenttemplate> 
            <div class="DivAlignLeft"></div> 
            <div class="DivAlignRight"></div> 
            <telerik:RadTreeView ID="AttributeTree" runat="server" Skin="Vista" EnableDragAndDrop="true" 
                OnNodeDrop="AttributesTree_HandleDrop" OnClientNodeDropping="onNodeDropping" 
                OnClientNodeDragging="onNodeDragging" MultipleSelect="true"
                <WebServiceSettings Path="ProductsScreen.aspx" Method="LoadNodes" /> 
            </telerik:RadTreeView> 
        </contenttemplate> 
    </asp:UpdatePanel> 
</asp:Content> 


Any help would be greatly appreciated. Please let me know if there is anything else you need.

~~ Alex


2 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 2
answered on 17 Jun 2009, 02:56 PM
I have solved the issue of making the color red on load. It was as simple as creating an entry in a .css file.

C#
    [WebMethod] 
    public static RadTreeNodeData[] LoadNodes(RadTreeNodeData node) // called each time a node is expanded 
    { 
        AttributeService.Attribute atts = new AttributeService.Attribute(); 
        List<RadTreeNodeData> result = new List<RadTreeNodeData>(); 
        DataSet ds = new DataSet(), ads = new DataSet(); 
        DataTable data = new DataTable(), attributeData = new DataTable(); 
        int WebP_Id = Int32.Parse(HttpContext.Current.Session["WebPID"].ToString()); 
        ArrayList RadNodeChanged = new ArrayList(); 
 
        ds = atts.GetChildrenByWebpId(WebP_Id, Convert.ToInt32(node.Value)); // gets the children for a specific item 
        data = ds.Tables[0]; 
 
        foreach (DataRow row in data.Rows) 
        { 
            RadTreeNodeData childNode = new RadTreeNodeData(); 
 
            childNode.Text = row["AttributeName"].ToString(); // adds the name and the text to "childNode" 
            childNode.Value = row["Attribute_Id"].ToString(); 
 
            if (atts.GetAllChildren(Convert.ToInt32(childNode.Value)).Tables[0].Rows.Count > 0) 
            { 
                childNode.ExpandMode = TreeNodeExpandMode.WebService; 
            } 
             
            attributeData = atts.GetAttributes(WebP_Id).Tables[0]; 
            foreach (DataRow dr in attributeData.Rows) 
            { 
                if (String.Compare(dr["AttributeId"].ToString(), childNode.Value) == 0) 
                { 
                    childNode.CssClass = "TreeNodeSelected";               
                } 
            }  
 
            result.Add(childNode); 
        } 
        return result.ToArray(); 
    } 

.css
.TreeNodeSelected 
    color:Red; 


Although now, I am having trouble getting the color changed back to default when the button to refresh the grid and tree is selected if any nodes were added. The .css portion doesn't respond, neither does a removal of all nodes through iteration then replenishing the tree. If anyone has any suggestions please let me know.


 ~~ Alex
0
Alex
Top achievements
Rank 2
answered on 17 Jun 2009, 08:06 PM
The solution to the second part was also fairly simple, I made a while loop to remove all nodes, and an AJAXControlManager in the aspx file to refresh the treeview when the grid was updated. This way, whenever the user refreshes the grid, it will also refresh the tree and set them back to default.

            case "RebindGrid":  
            // refresh grid back to original state, before any changes were made after hitting the save button 
                getWebP_Id = GetValidWebpId(SearchBox.Text); 
                DataSet ds = new DataSet(); 
 
                while (AttributeTree.Nodes.Count > 0) 
                { 
                    AttributeTree.Nodes[0].Remove(); 
                } 
 
                LoadRootAttributeNodes(AttributeTree, TreeNodeExpandMode.WebService); 
                CurrentAttributes(getWebP_Id); 
                break
            default
                break


problem solved...
<asp:Content ID="AttributesTree" runat="server" ContentPlaceHolderID="AttributesLeftContent"
    <telerik:RadAjaxManager runat="server" EnableAJAX="true"
          <AjaxSettings> 
             <telerik:AjaxSetting AjaxControlID="AttributeTree"
                <UpdatedControls> 
                   <telerik:AjaxUpdatedControl ControlID="AttributeTree" /> 
                </UpdatedControls> 
             </telerik:AjaxSetting>   
             <telerik:AjaxSetting AjaxControlID="CurrentAttributesList"
                <UpdatedControls> 
                   <telerik:AjaxUpdatedControl ControlID="AttributeTree" /> 
                </UpdatedControls> 
             </telerik:AjaxSetting> 
           </AjaxSettings>      
    </telerik:RadAjaxManager> 
    <asp:UpdatePanel id="AttributeUpdatePanel" runat="server" UpdateMode="Conditional"
        <contenttemplate> 
            <div class="DivAlignLeft"></div> 
            <div class="DivAlignRight"></div> 
            <telerik:RadTreeView ID="AttributeTree" runat="server" Skin="Vista" EnableDragAndDrop="true" 
                OnNodeDrop="AttributesTree_HandleDrop" OnClientNodeDropping="onNodeDropping" 
                OnClientNodeDragging="onNodeDragging" MultipleSelect="true"
                <WebServiceSettings Path="ProductsScreen.aspx" Method="LoadNodes" /> 
                <DataBindings> 
                    <telerik:RadTreeNodeBinding Expanded="false" /> 
                </DataBindings> 
            </telerik:RadTreeView> 
        </contenttemplate> 
    </asp:UpdatePanel> 
</asp:Content> 



Tags
TreeView
Asked by
Alex
Top achievements
Rank 2
Answers by
Alex
Top achievements
Rank 2
Share this question
or