views:

385

answers:

3

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:

  • Both Listboxes are DataBound to unique data (SqlDataSource).
  • I want to add items from ListBox1 to ListBox2 and vice versa, when a user clicks a button.
  • I want to be able to add multiple items to the ListBox (presume that multiple selection is turned on)
  • I want that to trigger an UpdateCommand on the DataSource.

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.

A: 

Add an event handler for ListBox2's DataBound event. That will trigger after the SQL data is bound and will give you an opportunity to add additional data.

Sam
I need to be able to add data when the button is clicked. If you have 1,2,3 in ListBox1 and 4, 5, 6 in ListBox2 ... then the user selects "2" and clicks add ... I want to be able to update the data in ListBox2 to include "2" and have it run the UpdateCommand on the DataSource.
Chris Nicol
A: 

it is not important that your listbox binded to a datacontext or not you should use

ListBox1.Items.Add(/*your listBox Item*/);// for example if you have a person listbox you should have - ListBox1.Items.Add(new Person(1,"Nasser","Hajloo");

just remember that you should add Items after binding I mean

ListBox1.DataSource = myDBList;
ListBox1.DataBind();
//some other code 
ListBox1.Items.Add(new Person(1,"Nasser","Hajloo");
ListBox1.DataBind();
Nasser Hadjloo
I've tried this, but after I call the DataBind method, it doesn't not call the UpdateCommand. So the DB doesn't get the new item that I added.
Chris Nicol
after adding the Item doListBox1.Updat(); // or validate(); and do not Databind(); so after tha if you like to add the added Itm t your dataayou can provide this at the Person class cnstructor in the case witch I mentioned before, I mean when evr you new a Person you add it to DataBase, and if you do not like to addit t database and just like to show it you can use it like what I saidbefore , just remove second line of code (DataBinde()) and let the final DataBind() remains.let me know whas happen
Nasser Hadjloo
It's the "just add to database" part that I'm trying to figure out with the DataSource. I want to do two-way binding with a datasource on a listbox (with multiple updates in one call). I don't want to add the item to the database in a constructor. I want to merely transfer from one dataSource to another.
Chris Nicol
ok, so as I underestand you have a list that it binded to a ListBox and it may bind to a GridView or someting else, so instead of adding the Item to ListBox1.Items then you should add your Item to your list witch will bind to the ListBox or whatevre else, like this ----- list myMailList = new list(); myMailList.Add("Chris Nicol"); ---- and if your list is a generic one then you sould have List<Person> myMailList = new myMailList<Person>(); -- myMailList.Item.add(new Person(1,"Chris","Nichol"); --- so whenever you want to add a Item you should add it to your List instead of ListBox or GridView
Nasser Hadjloo
the note is whenever you add anItem to list you have to do ListBox1.Update(); // or ListBox1.Invalidate(); -- if do not Update() the ListBox, the adddedItem do not appear in list , Let me know if your problem still remains
Nasser Hadjloo
+1  A: 

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.

Justin C
That has given me an idea, but I'm not sure it helps too much. I want to the DataBound items, but when the UI command is called I want to add from a separate source more items to the original data and then persist that to the DB. I've updated the question, I think I didn't explain it very well.
Chris Nicol
Chris - I saw your response to Sam and that also helped explain. I'm pretty slammed right now, but I can edit my answer later to give a better record, but I think you have it. You can change the DataSource property as much as you want and just call the Bind() function to repopulate. Just remember if you are using DataSourceID on the front end that when you switch to DataSource on the back end you also clear the DataSourceID property.
Justin C