2011年11月28日 星期一
ListBox 複選 左右移動 & 上下移動 LINQ應用
.aspx
<table border="0" cellpadding="0" cellspacing="0" width="50">
<tr>
<td style="width: 35%">
<asp:ListBox ID="LB_LIST1" runat="server" SelectionMode="Multiple"
Width="100px" Height="150px">
<asp:ListItem>Item 1.</asp:ListItem>
<asp:ListItem>Item 2.</asp:ListItem>
<asp:ListItem>Item 3.</asp:ListItem>
<asp:ListItem>Item 4.</asp:ListItem>
<asp:ListItem>Item 5.</asp:ListItem>
<asp:ListItem>Item 6.</asp:ListItem>
<asp:ListItem>Item 7.</asp:ListItem>
<asp:ListItem>Item 8.</asp:ListItem>
<asp:ListItem>Item 9.</asp:ListItem>
<asp:ListItem>Item 10.</asp:ListItem>
</asp:ListBox>
</td>
<td style="width: 15%">
<asp:Button ID="BT_RightAll" runat="server" Text="→|" CommandName="RightAll"
oncommand="ListBox_Change_Command" Width="30px" /><br />
<asp:Button ID="BT_Right" runat="server" Text="→" CommandName="Right"
oncommand="ListBox_Change_Command" Width="30px" /><br />
<asp:Button ID="BT_Left" runat="server" Text="←" CommandName="Left"
oncommand="ListBox_Change_Command" Width="30px" />
<asp:Button ID="BT_LeftAll" runat="server" Text="|←" CommandName="LeftAll"
oncommand="ListBox_Change_Command" Width="30px" />
</td>
<td style="width: 35%">
<asp:ListBox ID="LB_LIST2" runat="server" SelectionMode="Multiple"
Width="100px" Height="150px"></asp:ListBox>
</td>
<td style="width: 15%">
<asp:Button ID="LB_Up" runat="server" Text="↑" CommandName="Up"
oncommand="ListBox_Change_Command" Width="30px" /><br />
<asp:Button ID="LB_Down" runat="server" Text="↓" CommandName="Down"
oncommand="ListBox_Change_Command" Width="30px" />
</td>
</tr>
</table>
.cs
protected void ListBox_Change_Command(object sender, CommandEventArgs e)
{
String strTempText = String.Empty;
String strTempValue = String.Empty;
try
{
switch (e.CommandName)
{
// 移動到右邊
case "Right":
LB_LIST2.Items.AddRange(
(from ListItem LI in LB_LIST1.Items where LI.Selected select LI).ToArray<ListItem>());
for (int i = 0; i < LB_LIST1.Items.Count; i++)
{
if (LB_LIST1.Items[i].Selected)
{
LB_LIST1.Items.RemoveAt(i);
i--;
}
}
break;
// 全部移動到右邊
case "RightAll":
LB_LIST2.Items.AddRange(
(from ListItem LI in LB_LIST1.Items select LI).ToArray<ListItem>());
for (int i = 0; i < LB_LIST1.Items.Count; i++)
{
LB_LIST1.Items.RemoveAt(i);
i--;
}
break;
// 移動到左邊
case "Left":
LB_LIST1.Items.AddRange(
(from ListItem LI in LB_LIST2.Items where LI.Selected select LI).ToArray<ListItem>());
for (int i = 0; i < LB_LIST2.Items.Count; i++)
{
if (LB_LIST2.Items[i].Selected)
{
LB_LIST2.Items.RemoveAt(i);
i--;
}
}
break;
// 全部移動到左邊
case "LeftAll":
LB_LIST1.Items.AddRange(
(from ListItem LI in LB_LIST2.Items select LI).ToArray<ListItem>());
for (int i = 0; i < LB_LIST2.Items.Count; i++)
{
LB_LIST2.Items.RemoveAt(i);
i--;
}
break;
// 往上移動
case "Up":
for (int i = 0; i < LB_LIST2.Items.Count; i++)
{
if (LB_LIST2.Items[i].Selected && !i.Equals(0))
{
strTempText = LB_LIST2.Items[i - 1].Text;
strTempValue = LB_LIST2.Items[i - 1].Value;
LB_LIST2.Items[i - 1].Text = LB_LIST2.Items[i].Text;
LB_LIST2.Items[i - 1].Value = LB_LIST2.Items[i].Value;
LB_LIST2.Items[i].Text = strTempText;
LB_LIST2.Items[i].Value = strTempValue;
LB_LIST2.Items[i - 1].Selected = true;
LB_LIST2.Items[i].Selected = false;
}
}
break;
// 往下移動
case "Down":
for (int i = LB_LIST2.Items.Count - 1; i >= 0; i--)
{
if (LB_LIST2.Items[i].Selected && !i.Equals(LB_LIST2.Items.Count - 1))
{
strTempText = LB_LIST2.Items[i + 1].Text;
strTempValue = LB_LIST2.Items[i + 1].Value;
LB_LIST2.Items[i + 1].Text = LB_LIST2.Items[i].Text;
LB_LIST2.Items[i + 1].Value = LB_LIST2.Items[i].Value;
LB_LIST2.Items[i].Text = strTempText;
LB_LIST2.Items[i].Value = strTempValue;
LB_LIST2.Items[i + 1].Selected = true;
LB_LIST2.Items[i].Selected = false;
}
}
break;
}
}
catch { }
}
原始文章出自於此
沒有留言:
張貼留言