Category - Projects

Many To Many Tables

topics

Many to many tables present some difficulty when it comes to presenting their information in a format that can be modified.

Early on I knew I wanted to have a checkboxlist of all the topics that you could assign to a project or post, where the user could select which ones applied, and during an edit, the currently selected would already be checked.

Of course, this presents a lot of issues. You have to build the list of topics, and then check to see which ones are already selected, set those to selected, and then when updating, determine if they are not still selected, and if so remove them, as well as determine if any new ones were checked.

I solved this problem by using a combination of techniques.

Nested inside the edit detailsview in a templated field is a checkboxlist hooked up to an sqldatasource that pulls all of the available topics.

Then using Linq, in the code behind I used the datacontext in the databound event of the checkboxlist to determine which ones were checked.

And then in the detailsview's connected sqldatasource's updating event, I used datacontext again to update the selected topics.

Loops and branching If's... sheesh.. no problem! ;)

You must be logged in to comment.