Forum Moderators: open

Message Too Old, No Replies

Data Transfer between listboxes does nothing

         

seamus

3:11 am on May 6, 2003 (gmt 0)

10+ Year Member



Hi there,

I am trying to set up a situation where there are 2 list boxes on a form. Listbox 1 is populated via a db call. List box 2 is populated by selecting an item in listbox 1 and clicking the add button. Pretty straight forward except when I run the page I can click the button at will and the page just reloads.

Any ideas?

Code follows:

CodeBehind Page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim Userlist As New SystemFramework()
Dim userName As String
ListBox1.DataSource = Userlist.GetUsersRequest.QueryString("companyID"))
ListBox1.DataTextField = "lastName"
ListBox1.DataValueField = "pk_userID"
ListBox1.DataBind()
ListBox1.SelectedIndex = 0
End if
End Sub

Public Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addButton.Click
Dim Selected As String
If Not ListBox1.SelectedItem Is "" Then
Selected = ListBox1.SelectedItem.Text
If ListBox2.Items.FindByText(Selected) Is "" Then
ListBox2.Items.Add(Selected)
ListBox2.SelectedIndex = 0
ListBox1.Items.Remove(Selected)
End If
End If
End Sub

webform:

<form id="Form1" method="post" runat="server">

<asp:ListBox id="ListBox1" runat="server" Width="191px" Height="261px"></asp:ListBox>&nbsp;

<asp:Button id="removeButton" runat="server" Text=" <<< "></asp:Button><br>

<asp:Button id="addButton" runat="server" Text=" >>> " OnClick="addButton_Click"></asp:Button>

<asp:ListBox id="ListBox2" runat="server" Width="191px" Height="261px"></asp:ListBox>
</form>

Thanks

RainMaker

1:02 am on May 10, 2003 (gmt 0)

10+ Year Member



FindByText returns a null when it fails to find a match.
In your original code you were comparing an empty string to that null value. In VB you should compare null values to "Nothing".
See the code below for your Button1_Click event, it should fix your problem.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Selected As String
If Not ListBox1.SelectedItem Is "" Then
Selected = ListBox1.SelectedItem.Text
If ListBox2.Items.FindByText(Selected) Is Nothing Then
ListBox2.Items.Add(Selected)
ListBox2.SelectedIndex = 0
ListBox1.Items.Remove(Selected)
End If
End If
End Sub