Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have this code here from a webpart that creates a treeview from a document library.

For each item in the list it will create two child nodes called "Up" and "Down". When you click up, it should change the parent node (which is the item in the document library) item's field called 'Order' the value 555. But when I try this and then go to the item, the order value didn't change. Does anyone see the problem?

Thanks

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.IO;

namespace VisualWebPartProject1.VisualWebPart1 
{
    public partial class VisualWebPart1UserControl : UserControl 
    {
        protected String DocLib = "MyTestDocLib";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (siteStructure.FindNode(DocLib) == null)
            {
                create_tree();
            }
            siteStructure.SelectedNodeChanged += new EventHandler(tree_Select);
        }

        void tree_Select(Object sender, EventArgs e)
        {
            string sel_val = siteStructure.SelectedValue.Substring(0, 1);
            string item_path = siteStructure.SelectedValue.Substring(2, siteStructure.SelectedValue.Length - 2);

            Label1.Text = "got 1";
            if (sel_val.Equals("1")) // user clicked up
            {
                Label1.Text = "got 2";
                update_item(item_path);
            }
            else // user clicked down
            {
                Label1.Text = "got 1.5";
            }
        }

        protected void update_item(String item_path)
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            //Need to set AllowUnsafeUpdates to true so you use the code to update the list.
            web.AllowUnsafeUpdates = true;
            SPList List = web.Lists[DocLib];
            SPListItemCollection listItems = List.GetItems();
            Label1.Text = "got 3";
            foreach (SPListItem item in listItems)
            {
                Label1.Text = "got 4";

                if (item.File.ServerRelativeUrl.Equals(item_path))
                {
                    item["Order"] = 555;
                    item.Update();
                    Label1.Text = "got 5";
                    break;
                }
            }
            List.Update();
            web.Update();
        }

        protected void refreshbutton_Click(Object sender, EventArgs e)
        {
            siteStructure.Nodes.Clear();
            create_tree();
            Label1.Text = "Label";
        }

        protected void create_tree()
        {
            SPWeb thisWeb = null;
            thisWeb = SPContext.Current.Web;

            SPList myList = thisWeb.Lists[DocLib];
            SPFolder my_folder = myList.RootFolder;

            TreeNode node;
            node = new TreeNode(myList.Title, null, null, myList.DefaultViewUrl, "_blank");
            siteStructure.Nodes.Add(node);
            TreeNode parentNode = node;

            TreeNode sub_parent, current_leaf;

            foreach (SPFolder folder in my_folder.SubFolders)
            {
                node = new TreeNode(folder.Name, null, null, folder.ServerRelativeUrl, "_blank");
                parentNode.ChildNodes.Add(node);
                sub_parent = node;

                foreach (SPFile item in folder.Files)
                {
                    node = new TreeNode(item.Name, null, null, item.ServerRelativeUrl, "_blank");
                    sub_parent.ChildNodes.Add(node);
                    current_leaf = node;

                    node = new TreeNode("Up", "1_" + item.ServerRelativeUrl, null, null, null);
                    current_leaf.ChildNodes.Add(node);
                    node = new TreeNode("Down", "0_" + item.ServerRelativeUrl, null, null, null);
                    current_leaf.ChildNodes.Add(node);
                }
            }

            siteStructure.ExpandAll();
        }
    }
}
share|improve this question
    
Have you tried to attach a debugger? I'm pretty sure your tree_Select is never called as you only attach it to the event when siteStructure is empty. –  Per Jakobsen Aug 2 '12 at 19:46
    
@Per Jakobsen, I updated my code in the above post, I used a label to do some debugging, and I noticed that it said "got 5" after clicking on "Up". So I know it went into the part to update the order value. But after when I went to check the value, it was still the same... –  omega Aug 2 '12 at 20:12
    
Have you tried debugging it? –  Mike Aug 7 '12 at 17:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.