views:

351

answers:

2

I have a multiselect ListBox within a FormView. I can't figure out how to setup the databinding for it.
I can populate the listbox fine. If the listbox was a single select I could use say SelectValue='<%#Bind("col")%>' and that works fine. Is there something that I can bind to for a multiselect listbox? I've tried manually DataBinding by handling the DataBinding event of the listbox and setting the selected property on the appropriate items. Unfortunately, there are no items in the listbox during the DataBinding event. The closest thing I've found is to save the value that determines what items should be selecting during DataBinding and then use that value in the FormViews DataBinding event to select the right items which seems like a hack to me.

Is there a better way?


EDIT:

To clarify what I am currently doing...

I am using the FormViews's ItemCreated event to save the FormView's DataItem. Then in the FormView's DataBound event I find the listbox and manually set the selected items. It doesn't seem right that I have to save the value like this and I assume there is a more correct way to do this that I just can't see.

A: 

Did you try the ItemDataBound event?

Paulo Santos
That is for the ListView control. I'm using a ListBox in a FormView control.
drs9222
A: 

When I've done this in the past I've set the data source of the listbox, bind it, then iterate through the list of items in the listbox and set the .Selected to true for each item that matches my criteria or values. I'm not sure it's the most efficient, but for the small lists I've dealt with it certainly worked fine.

Thyamine
That is what I do now but I have to do it during the FormView's DataBound event because the items aren't there in the listbox's DataBinding event. Where do you do it?
drs9222
Depending on the situation I'd probably call the loadList() function during the page load (making sure it's not a postback), then in the loadList() function do the databinding. Right after the databinding, in the same loadList() function, then iterate through the list and mark the ones you need.
Thyamine
I just made a form needing to handle M:N relationships with my database. I did this same thing, except I just did it all in FormView_DataBound event. This would be a good case for a better ListBox/CheckBoxList control that handles multiple value binding.
subkamran