Listbox Problems

  • Thread starter Sparky Arbuckle
  • Start date
S

Sparky Arbuckle

The code below works to an extent. The problem with it is that no
matter which item is selected, the first one in the list is always the
item that gets added/removed. How can I alleviate this problem? I have
tried If Not Page.IsPostBack and still nothing.

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text()
lbColumn1.Items.Add(s)
lbColumn2.Items.Remove(s)

lblDisplay.Text = "Deleted: <b>" & lbColumn1.SelectedItem.Text & "</b>"

End Sub
 
S

S. Justin Gengo

Sparky,

Where did you put the If Not IsPostBack?

It sounds like you're databinding your list on every page load thus
resetting the selected item to the first.

The If Not IsPostBack should go around your databinding routine so that it
only occurrs the very first time the page is loaded.

Let me know if that isn't working.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Sparky Arbuckle

I've tried putting the If Not IsPosBack around the sub that binds the
listboxes and also around the event handler that adds/removes the
selected listbox items. I have been unsuccessful down both avenues.
 
S

S. Justin Gengo

Sparky,

Is it possible to show more of the code? Or a short example with static
items in the lists to work with?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Sparky Arbuckle

Here you go Justin:

This is the sub that handles the "Delete Drawings" button. It simply
hides / shows panels and populates the listbox.

Private Sub hlbtnDeleteDrawings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles hlbtnDeleteDrawings.Click

If Page.IsPostBack Then
PanelDeleteDrawings.Visible = True
PanelAddDrawings.Visible = False
PanelOptions.Visible = True
lblDrawingList.Text = "Delete drawings from: <b>" &
ddlDrawingList.SelectedItem.Text() & "</b>"
End If

PopulateListBox()

End Sub
_____________

This is the function that populates the listbox with data.

Function PopulateListBox()

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSettings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End Function
___________________________


This is the sub for the button to transfer Items from Column1 to
Column2:

Private Sub btnCol1ToCol2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCol1ToCol2.Click

Dim s As String = lbColumn1.SelectedItem.Text
lbColumn2.Items.Add(s)
lbColumn1.Items.Remove(s)

End Sub
 
S

S. Justin Gengo

Sparky,

A simple test shows that your code should be working. And that the problem
has to be that databinding is recurring somewhere.

Here's what I tested with.

WebForm Code:

<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox id="ListBox2" runat="server"></asp:ListBox>
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
</form>

Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'---First page load only
ListBox1.Items.Add(New ListItem("1"))
ListBox1.Items.Add(New ListItem("2"))
ListBox1.Items.Add(New ListItem("3"))
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItem.Text)
ListBox1.Items.Remove(ListBox1.SelectedItem.Text)
End Sub

Keeping the task simple like this the code works. The only thing that could
be creating the problem you are describing is if the list box is being
re-bound. Based on the code you sent me you should change your populate list
box line to:

Function PopulateListBox()

If Not IsPostBack Then

Dim strDLNumber As String = ddlDrawingList.SelectedItem.Text()

Dim objConn As New
OleDb.OleDbConnection(ConfigurationSettings.AppSettings("strConnection"))
Dim strSQL As String = "SELECT tblDrawings.DrawingNumber,
tblDrawings.ID FROM tblDrawingList INNER JOIN tblDrawings ON
tblDrawingList.ID = tblDrawings.ID WHERE (DLNumber = '" & strDLNumber &
"');"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

lbColumn1.DataSource = ds.Tables("DataTable")
lbColumn1.DataTextField = "DrawingNumber"
lbColumn1.DataValueField = "ID"
lbColumn1.DataBind()

End If

End Function


Of course there may be a better place in your code to put the If Not
IsPostBack, but this should work for testing...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

Sparky,

I've got it. (I'll post to the forum also.) I should have noticed that you
were setting you're list box item's text AND value.



Adding and removing an item by name as string works if just the text has
been set as in the example I sent you earlier, but if the items have both
text and value set you need to use the full list item in the add and remove
methods.



Like this:



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Try

Dim s As ListItem = ListBox1.SelectedItem



ListBox2.Items.Add(s)

ListBox1.Items.Remove(s)



ListBox1.SelectedIndex = -1

ListBox2.SelectedIndex = -1

Catch ex As Exception

Throw ex

End Try

End Sub



(Errors are thrown the next time you remove an item if you don't set the
list boxes selected item to none.)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top