Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Dec 01, 2010 @ 13:35
    Bo Damgaard Mortensen
    0

    Confused about ASP.NET CheckboxList

    Hi 'bracos!

    I'm a bit lost here working with an asp:CheckboxList. The list displays several categories that is related (using the relationship api) to a document. The member is then able to edit the document and change the selected values(categories) and save the document.

    So, in technical terms when a member clicks the 'Save' button I need to:

    • Check if the selected item/node is already related to the document, if not: make a new relation
    • For every item/node that's not selected, check to see if there is a relation to the document, if there is: delete it.
    I almost wrote it in pseudo right there and it should be fairly straight forward in my eyes. I have the following code:
    spot2CategoryRelations = Relation.GetRelations(nodeId, spot2Category);
            foreach (Relation r in spot2CategoryRelations)
            {
                relatedCategories.Add(new Node(r.Parent.Id));
            }
            Node relatedNode = null;
            foreach (ListItem l in cbMemberTypes.Items)
            {
                int i = 0;
                bool found = false;
                while (!found && i < relatedCategories.Count)
                {
                    if (l.Text.Equals(relatedCategories[i].Name))
                    {
                        relatedNode = relatedCategories[i];
                        found = true;
                    }
                    i++;
                }
                if (l.Selected)
                {
                    if(!Relation.IsRelated(relatedNode.Id, nodeId))
                    {                   
                        Relation.MakeNew(nodeId, relatedNode.Id, spot2Category, nodeId + " is related to " + relatedNode.Name);
                    }
                }
                else
                {
                    foreach (Relation r in spot2CategoryRelations)
                    {
                        if (Relation.IsRelated(nodeId, relatedNode.Id))
                        {
                            r.Delete();
                        }
                    }
                }
            }
    nodeId is the id of the document you are editing. I'm databinding my CheckboxList in the Page_Load event in a !Page.IsPostBack. The problem with the above code is:
    • It doesn't do anything about the relations. It's not creating one or deleting existent relations.
    • I don't like the nested loops! Is there a more elegant way of doing this, instead of my ninja-code? ;)
    Any help/hint on this is appreciated!
    Thanks in advance,
    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Dec 01, 2010 @ 23:07
    Bo Damgaard Mortensen
    0

    10 hours and I stil haven't found a solution to this problem :( just even more confused really. So, if anyone out there has got just the smallest hint on this, I will be more than happy! :)

    Thanks!

    Bo

  • Rick Mather 42 posts 124 karma points
    Dec 01, 2010 @ 23:14
    Rick Mather
    0

    I can't tell why it isn't working but if you want to simplify your code, you should be able to replace the entire 'while' block with this (untested):

    relatedNode = relatedCategories.FirstOrDefault(x => x.Name.Equals(l.Text));

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Dec 02, 2010 @ 01:37
    Bo Damgaard Mortensen
    1

    Beautiful! :)

    Solved it by doing this:

    foreach (ListItem l in cbMemberTypes.Items)
    {
        if (l.Selected)
        {
            Node relatedNode = ca.SingleOrDefault(x => x.Name.Equals(l.Text));
            if (!Relation.IsRelated(relatedNode.Id, nodeId))
            {
                Relation.MakeNew(relatedNode.Id, nodeId, spot2Category, nodeId + " is related to " + relatedNode.Name);
            }
        }
    }

    Which is way more elegant and simple than my previous ninja-code! Thanks a lot Rick :) honestly don't know why I didn't think of the lambda-thingy.. 

     

Please Sign in or register to post replies

Write your reply to:

Draft