If I have two listboxes, with a button between them, how do I update the Items of ListBox2 if ListBox2's items are databound?
<asp:ListBox runat="server" ID="ListBox1" DataSourceID="DataSource1"
DataTextField="Name" DataValueField="ID" SelectionMode="Multiple" />
<asp:Button runat="server" ID="addButton" onClick="addButton_Click" />
<asp:ListBox runat="server" ID="ListBox2" DataSourceID="DataSource2"
DataTextField="Name" DataValueField="ID" SelectionMode="Multiple" />
Also if I use the SelectionMode="Multiple", will I be able to Update the DataSource using an UpdateCommand that takes one item at a time?
EDIT:
Ok, to add some clarification:
So far the only way I'm able to accomplish this is by manually taking each item from the first listBox and adding it as a parameter to the DataSource's UpdateCommand and manually call the SqlDataSource.Update() method. This works, but it means that I either need to pass a delimited string for multiple selections or open multiple connections. What I'm looking for is a way to update the DataSource on the ListBox and once it's fully updated, then call the Bind/Update and persist the data back to the DB.
The first question is are the items you want to add that are outside of the binded DataSource static or dynamic. If you have a couple of items you want to add that will always be the same, like a "None" option, then you can add it in your ASP as a ListItem and set the AppendToDataSource property to True.
If you want to dynamically insert values along with the databound values, then you can set your DataSource value to a C# function and not set your DataSourceID. It would look like this.
<asp:ListBox runat="server" ID="ListBox2" DataSource='<%# myFunc() %>'
DataTextField="Name" DataValueField="ID" SelectionMode="Multiple" />
Then in your C# codefile you have
protected ListItem[] myFunc()
{
ListItem[] retVals;
//write your code here to get your data from DataSource2 and whatever other sources you need
//then create a ListItem for each row of data you want to show,
//set the text and the value attributes and return an array of all the values in the order you want
return retVals;
}
This will allow you to keep any DataBind call functionality you may have already developed since this function will get called every time the DataBind method of the object is called.