博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ListBox选项实现上下移动

Posted on 2008-06-20 13:32  LonelyStar  阅读(915)  评论(0编辑  收藏  举报
aspx页面
 1<form id="form1" runat="server">
 2    <div>
 3    <asp:ListBox ID="ListBox1" runat="server">
 4        <asp:ListItem Text ="aaa"></asp:ListItem>
 5       <asp:ListItem Text ="bbb"></asp:ListItem> 
 6    </asp:ListBox>
 7   
 8   <asp:Button runat="server" ID = "Button1" OnClick="Button1_Click" Text="Up" /> 
 9   <asp:Button runat="server" ID = "Button2" OnClick="Button2_Click" Text="Down" /> 
10    </div>
11    </form>

cs页面
 1protected void Button1_Click(object sender, EventArgs e)
 2        {
 3            //up
 4            if (this.ListBox1.SelectedItem == null)
 5            {
 6                ClientScript.RegisterStartupScript(this.GetType(), "test""<script>alert('Please select one item');</script>");
 7                return;
 8            }

 9
10            int index = this.ListBox1.SelectedIndex;
11            if (index != 0)
12            {
13                ListItem item = this.ListBox1.Items[index];
14                this.ListBox1.Items.Remove(this.ListBox1.Items[index]);
15                this.ListBox1.Items.Insert(index - 1, item);
16            }

17        }

18
19        protected void Button2_Click(object sender, EventArgs e)
20        {
21
22            if (this.ListBox1.SelectedItem == null)
23            {
24                ClientScript.RegisterStartupScript(this.GetType(), "test""<script>alert('Please select one item');</script>");
25                return;
26            }

27
28            //down
29            int index = this.ListBox1.SelectedIndex;
30            int maxIndex = this.ListBox1.Items.Count - 1;
31            if (index != maxIndex)
32            {
33                ListItem item = this.ListBox1.Items[index];
34                this.ListBox1.Items.Remove(item);
35                this.ListBox1.Items.Insert(index + 1, item);
36            }

37
38        }