Forum Moderators: open
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>
<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
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