[原创]两个listbox之间操作数据交换的问题!

不好意思,前段写的listbox互操作的例子出了点问题!现在给的这个例子就完全可以解决问题了!解决方法是:

就是可以把a的listbox项添加到b的listbox.然后删除a添加过去的项.代码如下:

Public Class TestInsertInto
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents ListBox2 As System.Web.UI.WebControls.ListBox
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        If Not IsPostBack Then
            bindlb()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim arrl As ArrayList = New ArrayList
        For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).Selected = True Then
                ListBox2.Items.Add(ListBox1.Items(i))
                ListBox2.SelectedIndex = -1
                arrl.Add(ListBox1.Items(i))
            End If
        Next
        If arrl.Count >= 1 Then
            For j As Integer = 0 To arrl.Count - 1
                ListBox1.Items.Remove(arrl.Item(j))
            Next
        End If
    End Sub

    Sub bindlb()
        Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("shujuku"))
        Dim cmd As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("select id,fenshu from test_fs", conn)
        Dim ds As DataSet = New DataSet
        cmd.Fill(ds, "tab")
        ListBox1.DataSource = ds.Tables("tab")
        ListBox1.DataTextField = "fenshu"
        ListBox1.DataValueField = "id"
        ListBox1.DataBind()
    End Sub
End Class

以下为.aspx文件代码:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TestInsertInto.aspx.vb" Inherits="WebApplication4.TestInsertInto"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>TestInsertInto</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:listbox id="ListBox1" style="Z-INDEX: 101; LEFT: 97px; POSITION: absolute; TOP: 22px" runat="server"
     SelectionMode="Multiple" Width="163px" Height="111px"></asp:listbox><asp:button id="Button1" style="Z-INDEX: 102; LEFT: 207px; POSITION: absolute; TOP: 154px" runat="server"
     Width="98px" Height="36px" Text="Button"></asp:button><asp:listbox id="ListBox2" style="Z-INDEX: 103; LEFT: 275px; POSITION: absolute; TOP: 21px" runat="server"
     SelectionMode="Multiple" Width="174px" Height="102px"></asp:listbox><asp:button id="Button2" style="Z-INDEX: 104; LEFT: 344px; POSITION: absolute; TOP: 163px" runat="server"
     Width="80px" Height="39px" Text="Button"></asp:button>
    <asp:LinkButton OnClick="javascript:window.open('new.aspx','newWin','width=300,height=200');"
 id="LinkButton1" style="Z-INDEX: 105; LEFT: 183px; POSITION: absolute; TOP: 208px"
     runat="server" Height="22px" Width="79px">LinkButton</asp:LinkButton></FONT></form>
 </body>
</HTML>

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭