如何处理 ASP.NET ListBox 上 SqlDataSource 的双向绑定

发布于 2024-08-17 01:46:19 字数 977 浏览 13 评论 0原文

如果我有两个列表框,它们之间有一个按钮,如果 ListBox2 的项目是数据绑定的,如何更新 ListBox2 的项目?

<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" />

另外,如果我使用 SelectionMode="Multiple",我是否能够使用一次获取一项的 UpdateCommand 来更新数据源?

编辑:

好的,添加一些说明:

  • 两个列表框都是数据绑定到唯一数据(SqlDataSource)。
  • 当用户单击按钮时,我想将 ListBox1 中的项目添加到 ListBox2 中,反之亦然。
  • 我希望能够向列表框添加多个项目(假设已打开多项选择)
  • 我希望触发数据源上的 UpdateCommand。

到目前为止,我能够实现此目的的唯一方法是手动从第一个列表框中获取每个项目并将其作为参数添加到 DataSource 的 UpdateCommand 中,然后手动调用 SqlDataSource.Update() 方法。这是可行的,但这意味着我要么需要传递一个分隔字符串以进行多个选择,要么打开多个连接。我正在寻找的是一种更新 ListBox 上的数据源的方法,一旦完全更新,然后调用绑定/更新并将数据保留回数据库。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-08-24 01:46:19

第一个问题是您要添加的项目是在绑定的静态或动态数据源之外。如果您想要添加几个始终相同的项目(例如“None”选项),那么您可以将其作为 ListItem 添加到 ASP 中,并将 AppendToDataSource 属性设置为 True。

如果要动态插入值以及数据绑定值,则可以将 DataSource 值设置为 C# 函数,而不设置 DataSourceID。它看起来像这样。

<asp:ListBox runat="server" ID="ListBox2" DataSource='<%# myFunc() %>'
 DataTextField="Name" DataValueField="ID" SelectionMode="Multiple" />

然后在您的 C# 代码文件中,

    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;    
         }

这将允许您保留您可能已经开发的任何 DataBind 调用功能,因为每次调用对象的 DataBind 方法时都会调用此函数。

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.

不羁少年 2024-08-24 01:46:19

为 ListBox2 的 DataBound 事件添加事件处理程序。这将在 SQL 数据绑定后触发,并为您提供添加其他数据的机会。

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.

鹿港小镇 2024-08-24 01:46:19

您的列表框是否绑定到数据上下文并不重要,您应该使用它,

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");

只需记住您应该在绑定后添加项目我的意思是

ListBox1.DataSource = myDBList;
ListBox1.DataBind();
//some other code 
ListBox1.Items.Add(new Person(1,"Nasser","Hajloo");
ListBox1.DataBind();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文