常用功能:在ListBox间移动Items,改变Items的顺序

刚刚为了解决一个问题写了一个比较常用的功能:从一个ListBox中选择(转移)Items到另一个ListBox,改变Items在ListBox中的顺序.
截图如下:
常用功能:在ListBox间移动Items,改变Items的顺序_第1张图片

实现代码如下:
<% @ Page Language="VB"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< script  runat ="server" >

    Protected Sub btnMoveUp_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Item As New ListItem
        Dim PrevIndex As Integer
        
'Move sites order of preference in up direction
        If lstSelectedSites.GetSelectedIndices.Length = 1 Then
            If lstSelectedSites.SelectedIndex <> -1 Then

                Item.Value = lstSelectedSites.SelectedValue
                Item.Text = lstSelectedSites.SelectedItem.ToString
                PrevIndex = CInt(lstSelectedSites.SelectedIndex) - 1
                If PrevIndex = -1 Then
                    Exit Sub
                End If
                lstSelectedSites.Items.Remove(lstSelectedSites.SelectedItem)
                lstSelectedSites.Items.Insert(PrevIndex, Item.Text)
                lstSelectedSites.Items.Item(PrevIndex).Value = Item.Value
                lstSelectedSites.SelectedIndex = PrevIndex

            End If
        End If
    End Sub

    Protected Sub btnMoveDown_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Item As New ListItem
        Dim NextIndex As Integer
        
'Move sites order of preference in up direction
        If lstSelectedSites.GetSelectedIndices.Length 
= 1 Then
            If lstSelectedSites.SelectedIndex 
<> -1 Then

                Item.Value 
= lstSelectedSites.SelectedValue
                Item.Text 
= lstSelectedSites.SelectedItem.ToString
                NextIndex 
= CInt(lstSelectedSites.SelectedIndex) + 1
                If NextIndex 
= lstSelectedSites.Items.Count Then
                    Exit Sub
                End If
                lstSelectedSites.Items.Remove(lstSelectedSites.SelectedItem)
                lstSelectedSites.Items.Insert(NextIndex, Item.Text)
                lstSelectedSites.Items.Item(NextIndex).Value 
= Item.Value
                lstSelectedSites.SelectedIndex 
= NextIndex

            End If
        End If
    End Sub

    Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Item As New ListItem
        Dim j As Integer 
= lstAllSites.GetSelectedIndices().Length
        
'Move sites order of preference in up direction
        If j > 0 Then
            Dim i As Integer
            i = 0
            Do While (i < j)
                lstSelectedSites.Items.Insert(lstSelectedSites.Items.Count, lstAllSites.Items.Item(lstAllSites.GetSelectedIndices(i)))
                lstAllSites.Items.Remove(lstAllSites.Items.Item(lstAllSites.GetSelectedIndices(i)))
                j -= 1
            Loop
        End If
    End Sub

    Protected Sub btnUnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Item As New ListItem
        Dim j As Integer = lstSelectedSites.GetSelectedIndices().Length
        
'Move sites order of preference in up direction
        If j 
> 0 Then
            Dim i As Integer
            i 
= 0
            Do While (i 
< j)
                lstAllSites.Items.Insert(lstAllSites.Items.Count, lstSelectedSites.Items.Item(lstSelectedSites.GetSelectedIndices(i)))
                lstSelectedSites.Items.Remove(lstSelectedSites.Items.Item(lstSelectedSites.GetSelectedIndices(i)))
                j 
-= 1
            Loop
        End If
    End Sub
</ script >

< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  id ="Head1"  runat ="server" >
    
< title > Untitled Page </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:ScriptManager  ID ="ScriptManager1"  runat ="server" >
        
</ asp:ScriptManager >
        
< asp:UpdatePanel  ID ="UpdatePanel1"  runat ="server"  UpdateMode ="Conditional"  
            ChildrenAsTriggers
="False" >
            
< ContentTemplate >
                
< table  style ="width: 100%;" >
                    
< tr >
                        
< td >
                            
< asp:UpdatePanel  UpdateMode ="Conditional"  ID ="UPanellstAllSites"  runat ="server" >
                                
< ContentTemplate >
                                    
< asp:ListBox  BorderStyle ="None"  BackColor ="white"  ID ="lstAllSites"  runat ="server"
                                        Height
="251px"  Width ="258px"  Style ="border: none;"  SelectionMode ="Multiple" >
                                        
< asp:ListItem  Value ="1"  Text ="1"   />
                                        
< asp:ListItem  Value ="2"  Text ="2"   />
                                        
< asp:ListItem  Value ="3"  Text ="3"   />
                                        
< asp:ListItem  Value ="4"  Text ="4"   />
                                        
< asp:ListItem  Value ="5"  Text ="5"   />
                                        
< asp:ListItem  Value ="6"  Text ="6"   />
                                        
< asp:ListItem  Value ="7"  Text ="7"   />
                                        
< asp:ListItem  Value ="8"  Text ="8"   />
                                        
< asp:ListItem  Value ="9"  Text ="9"   />
                                        
< asp:ListItem  Value ="10"  Text ="10"   />
                                    
</ asp:ListBox >
                                
</ ContentTemplate >
                            
</ asp:UpdatePanel >
                        
</ td >
                        
< td >
                            
< asp:Button  ID ="btnSelect"  runat ="server"  Text ="Select"  OnClick ="btnSelect_Click"   />
                            
< asp:Button  ID ="btnMoveUp"  runat ="server"  Text ="MoveUp"  OnClick ="btnMoveUp_Click"   />
                            
< asp:Button  ID ="btnMoveDown"  runat ="server"  Text ="MoveDown"  OnClick ="btnMoveDown_Click"   />
                            
< asp:Button  ID ="btnUnSelect"  runat ="server"  Text ="UnSelect"  OnClick ="btnUnSelect_Click"   />
                        
</ td >
                        
< td >
                            
< asp:UpdatePanel  UpdateMode ="Conditional"  ID ="UpdatePanel2"  runat ="server" >
                                
< ContentTemplate >
                                    
< asp:ListBox  BorderStyle ="None"  ID ="lstSelectedSites"  runat ="server"  Height ="291px"
                                        Width
="258px"  Style ="background-color: #c6c6ce; border: none; text-decoration: none;
                                        text-align: center;"
 SelectionMode ="Multiple" ></ asp:ListBox >
                                
</ ContentTemplate >
                                
< Triggers >
                                    
< asp:AsyncPostBackTrigger  ControlID ="btnMoveDown"   />
                                    
< asp:AsyncPostBackTrigger  ControlID ="btnMoveUp"   />
                                
</ Triggers >
                            
</ asp:UpdatePanel >
                        
</ td >
                    
</ tr >
                
</ table >
            
</ ContentTemplate >
            
< Triggers >
                
< asp:AsyncPostBackTrigger  ControlID ="btnSelect"   />
                
< asp:AsyncPostBackTrigger  ControlID ="btnUnSelect"   />
            
</ Triggers >
        
</ asp:UpdatePanel >
    
</ div >
    
</ form >
</ body >
</ html >

这个功能在Winform里很常用的.

你可能感兴趣的:(listbox)