Showing posts with label listbox. Show all posts
Showing posts with label listbox. Show all posts

Saturday, March 31, 2012

Strang behaviour of multiselection Listbox

Hello,

i´d like to get access to the selected items of my listbox...

I did this in a loop, but I get only acces to the first selected item of the Listbox.

Has anyone solved the same problem? I really need help. Thx in advance.

Code (VB.net):

dim el as listitemfor each el in mylistbox.Items if el.selected then 'do something end ifnext

Give this a try...

 iCnt = 0 Dim li As ListItem For Each li In myListBox.Items If li.Selected Then iCnt += 1 Next If iCnt <> 0 Then Dim i As Integer iCnt = myListBox.Items.Count For i = 0 To iCnt - 1 If myListBox.Items(i).Selected Then 'Do Something End If Next
 End If
 Zath

Still the same problemSad
I´m sad...

But thank you for the try...

That shouldn't be.

Do you have this line in there?

If myListBox.Items(i).Selected Then

Zath


Yes...

No matter what I do, the first selected item returns true for .selected

the next ones return false...


ListBoxes (and CheckBoxLists) expose a new property called "GetSelectedIndices()"

So you can say

Dim LI As ListItem
For Each idx As Int32 in mylistbox.GetSelectedIndices()
LI = mylistbox.Items(idx)
'Do what you need to do with this selected list item
Next

*If* that doesn't work for you, and there is absolutely no reason why this code or the code you posted shouldn't... then you have other issues, like for instance, re-binding the list on every page load (which would "reset" the listboxbefore your event handler got a hold of the control)


I´m working on 1.1 so that the above function is not available for me...


But I will check the second issue. Thanks for the hint...


That did the trick... Sorry for the wasting of your time...

Did a wrong binding on PostBacks so I killed all of the selections except of one in the listbox...

*smashing head against the next wall*


i can't speak for the others replying on this topic... but you didn't/aren't wasting my time... glad to help out...

the whole binding cylce is a huge "gotcha" in .NET, we've all had our battles with it


(this is where you tell them "but it sure is great thatEasyListBox doesn't have that problem" Big Smile )

Thursday, March 22, 2012

Strange Listbox Behavior when using a Datasource

I have 2 Listboxes placed nexto each other with buttons bellow them to Add, Add All, Remove, Remove All.

Both list boxes use a select query from a SQL 2005 table to get their values.

Thats about up to where things behave normally. If I click Add, it should remove the selected item from listbox1 and add it to listbox2. It adds the item to listbox2, but doesnt remove it from listbox1. If however, I click remove on the newly added item, it will remove that item and place it back in listbox1, except there are now two entries in listbox1 with the same name/value. The add all and remove all buttons cause the browser to "get stuck", im assuming because it gets stuck in a endless loop because it can never remove the values from listbox1.

Any help would be GREATLY appreciated, this has been driving me up the wall for the better part of the day.

Some code snippets:

<asp:ListBox ID="AvailableManufacturers" runat="server" DataSourceID="SqlDataSource1" DataTextField="manufacturerName"
DataValueField="manufacturerID" SelectionMode="Multiple"></asp:ListBox>
<asp:ListBox ID="CurrentManufacturers" runat="server" DataSourceID="SqlDataSource2" DataTextField="manufacturerName"
DataValueField="manufacturerID" SelectionMode="Multiple"></asp:ListBox>

<asp:Button ID="RemoveAll" runat="server" Text="<<" OnClick="RemoveAll_Click"/>
<asp:Button ID="Remove" runat="server" Text="<" OnClick="Remove_Click"/>

<asp:Button ID="Add" runat="server" OnClick="Add_Click" Text=">"/>
<asp:Button ID="AddAll" runat="server" OnClick="AddAll_Click" Text=">>"/><br />
<br />
<asp:Button ID="Update" runat="server" BackColor="#FFFFC0" BorderColor="Maroon"
BorderStyle='Ridge' ForeColor='Red' Text='Update' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString1 %>"
SelectCommand="SELECT manufacturerID, manufacturerName FROM manufacturers WHERE (manufacturerID NOT IN (SELECT DISTINCT manufacturerID FROM dealerManufacturers WHERE (dealerUsername = @dotnet.itags.org.dealerUsername)))">
<SelectParameters>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString1 %>"
SelectCommand="SELECT manufacturerID, manufacturerName FROM manufacturers WHERE (manufacturerID IN (SELECT DISTINCT manufacturerID FROM dealerManufacturers WHERE (dealerUsername = @dotnet.itags.org.dealerUsername)))">
<SelectParameters>
</SelectParameters>
</asp:SqlDataSource>

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("dealerUsername", TypeCode.String, User.Identity.Name);
SqlDataSource2.SelectParameters.Add("dealerUsername", TypeCode.String, User.Identity.Name);

}

protected void Add_Click(Object Src, EventArgs E)
{

if (AvailableManufacturers.SelectedIndex != -1)
{

CurrentManufacturers.Items.Add(new ListItem(AvailableManufacturers.SelectedItem.Text));
AvailableManufacturers.Items.Remove(AvailableManufacturers.SelectedItem.Text);
}
}

protected void AddAll_Click(Object Src, EventArgs E)
{

while (AvailableManufacturers.Items.Count != 0)
{

CurrentManufacturers.Items.Add(new ListItem(AvailableManufacturers.Items[0].Text));
AvailableManufacturers.Items.Remove(AvailableManufacturers.Items[0].Text);
}
}

protected void Remove_Click(Object Src, EventArgs E)
{

if (CurrentManufacturers.SelectedIndex != -1)
{

AvailableManufacturers.Items.Add(new ListItem(CurrentManufacturers.SelectedItem.Text));
CurrentManufacturers.Items.Remove(CurrentManufacturers.SelectedItem.Text);
}
}

protected void RemoveAll_Click(Object Src, EventArgs E)
{

while (CurrentManufacturers.Items.Count != 0)
{

AvailableManufacturers.Items.Add(new ListItem(CurrentManufacturers.Items[0].Text));
CurrentManufacturers.Items.Remove(CurrentManufacturers.Items[0].Text);
}
}

You have to iterate in reverse order (bottom to top) through all the selected items of the first listbox and add them to the second listbox and then remove them from the first listbox. Doing in reverse order is very important since you have to remove them based on an index. If you do it in a normal order, then after you remove the first item (let's say index 3) then all the indexes change and you might get an index outside of the boundaries exception.

Having that said you will implement the add button this way:

protected void Add_Click(object sender, EventArgs e) {for (int i = AvailableManufacturers.Items.Count - 1; i >= 0; i--) {if (AvailableManufacturers.Items[i].Selected)//if this item was selected { CurrentManufacturers.Items.Add(AvailableManufacturers.Items[i].Text); AvailableManufacturers.Items.RemoveAt(i); } } }
The same thing applies for the remove, but with the opposite listboxes and for the remove all and add all you do the same thing, but doin't check for selected.

..and if that drives you nuts, there are components likeEasyListBox and Metabuilders'DualList that will do all this for you :)