[.NET]兩個ListBox,左右搬移選項
今天實作ASP.NET 4.0 專題實務[I] ListBox搬移的練習,
內容如下:
ASPX
<asp:ListItem>左一</asp:ListItem>
<asp:ListItem>左二</asp:ListItem>
<asp:ListItem>左三</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Height="125px" Width="85px">
<asp:ListItem>右A</asp:ListItem>
<asp:ListItem>右B</asp:ListItem>
<asp:ListItem>右C</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button右移==>" />
<asp:Button ID="Button2" runat="server" Text="<==Button左移" />
VB
'== 右移 ==
Dim a As Integer = 0
For i As Integer = 0 To (ListBox1.Items.Count - 1)
If ListBox1.Items(i).Selected Then '==判定哪一個子選項被點選了。
ListBox2.Items.Add(ListBox1.Items(i).Text)
a = a + 1
ListBox1.Items.Remove(ListBox1.Items(i).Text)
'==被搬移走了,這個子選項就該移除!
End If
Next
If a = 0 Then
Label1.Text = "<font color=red>警告!您未點選任何一個子選項</font>"
Else
Label1.Text = "<font color=blue>移動成功</font>"
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'== 左移 ==
Dim b As Integer = 0
For j As Integer = 0 To (ListBox2.Items.Count - 1)
If ListBox2.Items(j).Selected Then '==判定哪一個子選項被點選了。
ListBox1.Items.Add(ListBox2.Items(j).Text)
b = b + 1
ListBox2.Items.Remove(ListBox2.Items(j).Text)
'==被搬移走了,這個子選項就該移除!
End If
Next
If b = 0 Then
Label2.Text = "<font color=red>警告!您未點選任何一個子選項</font>"
Else
Label2.Text = "<font color=green>移動成功</font>"
End If
End Sub
DataSourceID="ds_HR_EDU_TYPE" DataTextField="COD_ID_VAL"
DataValueField="COD_ID" SelectionMode="Multiple">
</asp:ListBox>
<asp:SqlDataSource ID="ds_HR_EDU_TYPE" runat="server"
ConnectionString="<%$ ConnectionStrings:Chilin_AppConnectionString %>"
ProviderName="<%$ ConnectionStrings:Chilin_AppConnectionString.ProviderName %>"
SelectCommand="SELECT COD_ID,COD_VAL,COD_ID_VAL,CMP_SERIL_NO FROM fnCODE('HR_EDU_TYPE')
WHERE CMP_SERIL_NO='A001'"></asp:SqlDataSource>
DataSourceID="ds_HR_FOURKINDS" DataTextField="COD_ID_VAL"
DataValueField="COD_ID" SelectionMode="Multiple">
</asp:ListBox>
<asp:SqlDataSource ID="ds_HR_FOURKINDS" runat="server"
ConnectionString="<%$ ConnectionStrings:Chilin_AppConnectionString %>"
SelectCommand="SELECT COD_ID,COD_VAL,COD_ID_VAL,CMP_SERIL_NO
FROM fnCODE('HR_FOURKINDS')
WHERE CMP_SERIL_NO='A001'"></asp:SqlDataSource>