开发者

How to handle two-way binding of a SqlDataSource on a ASP.NET ListBox

If I have two listboxes, with a button between them, how do I update the Items of ListBox2 if Li开发者_StackOverflow社区stBox2'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.


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.


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜