在页面加载时向ListBox添加项目,并在按钮单击时将项目传输到另一个ListBox

时间:2016-04-28 14:12:01

标签: asp.net vb.net

'本规范不起作用。请帮忙。 '不确定这里需要做什么。

Public Class WebForm2
    Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ListBox1.Items.Add("test")
    ListBox1.Items.Add("test2")
End Sub

Protected Sub AddOneButton0_Click(sender As Object, e As EventArgs) Handles AddOneButton0.Click
    If (ListBox1.SelectedIndex = -1) Then
        MsgBox("please select an item")
    Else
        ListBox2.Items.Add(ListBox1.SelectedItem)
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End If
End Sub

结束班

'添加HTML以帮助弄清楚我的代码是否有任何问题。

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm2.aspx.vb" Inherits="GBATExcel.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <!DOCTYPE html>

<html>
<head>
    <title></title>
</head>
<body>
    <div style="height: 904px">

        <asp:ListBox ID="ListBox1" runat="server" style="position:absolute; top: 101px; left: 28px; height: 170px; width: 111px;"></asp:ListBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;

        <br />

        <asp:ListBox ID="ListBox2" runat="server" style="position:absolute; top: 103px; left: 223px; height: 167px; width: 131px;"></asp:ListBox>
        <asp:Button ID="RemoveAllButton" runat="server" Text="&lt;&lt;" style="position:absolute; top: 435px; left: 153px;"/>

        <asp:Button ID="AddOneButton2" runat="server" Text="&gt;" style="position:absolute; top: 308px; left: 157px;"/>

        <asp:Button ID="RemoveOneButton2" runat="server" Text="&lt;" style="position:absolute; top: 565px; left: 156px;"/>

        <asp:Button ID="AddAllButton" runat="server" Text="&gt;&gt;" style="position:absolute; top: 194px; left: 153px;"/>

        <asp:ListBox ID="ListBox3" runat="server" style="position:absolute; top: 302px; left: 29px; height: 177px; width: 106px;"></asp:ListBox>

        <asp:ListBox ID="ListBox4" runat="server" style="position:absolute; top: 520px; left: 223px; height: 170px; width: 129px;"></asp:ListBox>

        <asp:Button ID="AddOneButton0" runat="server" Text="&gt;" style="position:absolute; top: 110px; left: 157px;"/>

        <asp:Button ID="RemoveOneButton0" runat="server" Text="&lt;" style="position:absolute; top: 151px; left: 158px;"/>

        <asp:Button ID="RemoveAllButton0" runat="server" Text="&lt;&lt;" style="position:absolute; top: 233px; left: 154px;"/>

        <asp:ListBox ID="ListBox5" runat="server" style="position:absolute; top: 730px; left: 28px; height: 170px; width: 111px;"></asp:ListBox>

        <asp:ListBox ID="ListBox6" runat="server" style="position:absolute; top: 302px; left: 223px; height: 171px; width: 128px;"></asp:ListBox>

        <asp:Button ID="AddOneButton3" runat="server" Text="&gt;" style="position:absolute; top: 737px; left: 156px;"/>

        <asp:Button ID="RemoveOneButton3" runat="server" Text="&lt;" style="position:absolute; top: 350px; left: 157px;"/>

        <asp:Button ID="AddAllButton1" runat="server" Text="&gt;&gt;" style="position:absolute; top: 396px; left: 153px;"/>

        <asp:Button ID="RemoveAllButton1" runat="server" Text="&lt;&lt;" style="position:absolute; top: 648px; left: 152px;"/>

        <asp:ListBox ID="ListBox7" runat="server" style="position:absolute; top: 520px; left: 29px; height: 170px; width: 110px;"></asp:ListBox>

        <asp:ListBox ID="ListBox8" runat="server" style="position:absolute; top: 729px; left: 222px; height: 170px; width: 134px;"></asp:ListBox>

        <asp:Button ID="AddOneButton4" runat="server" Text="&gt;" style="position:absolute; top: 525px; left: 156px;"/>

        <asp:Button ID="RemoveOneButton4" runat="server" Text="&lt;" style="position:absolute; top: 776px; left: 156px;"/>

        <asp:Button ID="AddAllButton2" runat="server" Text="&gt;&gt;" style="position:absolute; top: 607px; left: 152px;"/>

        <asp:Button ID="AddAllButton0" runat="server" Text="&gt;&gt;" style="position:absolute; top: 819px; left: 152px;"/>

        <asp:Button ID="RemoveAllButton2" runat="server" Text="&lt;&lt;" style="position:absolute; top: 864px; left: 152px;"/>

    </div>
    </body>
    </html>
</asp:Content>

&#39;基本上我遇到了麻烦,想要一些帮助

2 个答案:

答案 0 :(得分:3)

您应该在Not IsPostBack条件中添加项目:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ListBox1.Items.Add("test")
        ListBox1.Items.Add("test2")
    End If
End Sub

在将ListBox1添加到ListBox2之前删除该项目:

Protected Sub AddOneButton0_Click(sender As Object, e As EventArgs) Handles AddOneButton0.Click
    If (ListBox1.SelectedIndex = -1) Then
        MsgBox("please select an item")
    Else
        Dim item As ListItem = ListBox1.SelectedItem
        ListBox1.Items.Remove(item)
        ListBox2.Items.SelectedIndex = -1
        ListBox2.Items.Add(item)
    End If
End Sub

注意:在ListBox2中插入新项目之前,必须重置所选项目

答案 1 :(得分:1)

您需要将项目添加到ListBox2然后从ListBox1中删除

Protected Sub AddOneButton0_Click(sender As Object, e As EventArgs) Handles AddOneButton0.Click
        If (ListBox1.SelectedIndex = -1) Then
            MsgBox("please select an item")
        Else
            ListItem item = ListBox1.SelectedItem
            ListBox2.Items.Add(item)
            ListBox1.Items.Remove(item)

        End If
    End Sub