473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

.Selected property for listbox does not work

Hi
I have written a code for showing the list boxes as selected using a
Listitem and the selected property of the items.
Now I have 2 list boxes in my page. But it shows only the selected values
of the last list box in both the list boxes.
If i reverse the calls to the filllistbox methods it shows the value
selected for the 1st list box in both the listboxes.
I have the code for my method below. Is there a way I can solve this
problem?

The method calls are as follows :-

this.FillListBox(myModel.CustomerLBUserList, myModel.SelectedPRList,
this.lstProofreaders);

this.FillListBox(myModel.CustomerLBUserList, myModel.SelectedRevList,
this.lstReviewers);
Thanks in advance
Vipin Kedia
vi********@hotmail.com

-------------------------------------------------

/// <summary>
/// This is overloaded method.
/// This method fills the data as well as preselects item.
/// Note: This method will throw exception if selection mode is single
and selection arraylist contains multiple items for selection
/// </summary>
/// <param name="alData">The ArrayList of ListItem with which ListBox
needs to be populated</param>
/// <param name="alSelectionIds">The ArrayList of Ids which needs to be
selected</param>
/// <param name="lstFill">The ListBox</param>
public void FillListBox(ArrayList alData, ArrayList alSelectionIds,
ListBox lstFill) {
ListItem liAdd = null;
try {
lstFill.ClearSelection();
lstFill.Items.Clear();
for (int i=0; i<alData.Count; i++) {
liAdd = (ListItem) alData[i];
lstFill.Items.Add(liAdd);
lstFill.Items[i].Selected = false;
if (alSelectionIds.Contains(liAdd.Value)) {
lstFill.Items[i].Selected = true;
}
}
}
catch (Exception ex) {
throw ex;
}
finally {
liAdd = null;
}
}

-------------------------------------------------
Jul 21 '05 #1
8 2805
Vipin,

What code are you now showing, the original or the one who shows everything
in both boxes?
I gues the last.

Cor
Jul 21 '05 #2
Cor,

Yes u r right. There is no such code as original. This is the only code
which is not working.
If I run application in debug and loop through the for and if loops it
shows proper values being selected. But finally it shows the data for the
last listbox only.

-Vipin

"Cor Ligthert" wrote:
Vipin,

What code are you now showing, the original or the one who shows everything
in both boxes?
I gues the last.

Cor

Jul 21 '05 #3
Vipin,

Is this a webform or a winform project. I have assumed a winform project
what is the default in these newsgroups when it is not explicitly told or it
explictly in a aspnet newsgroup..

But your sample would not work why I did not try it in aspnet.

It seems for me not really a webform approach however you never know.

However I made a working sample for a winform what I thought that your goal
was

When it is for webform maybe can you make the same sample and try that.

(In my opinion there are not more than 4 rows to change)

I hope this helps?

Cor

\\\\
private void Form2_Load(object sender, System.EventArgs e)
{
ArrayList myArr1 = new ArrayList();
ArrayList myArrA = new ArrayList();
ArrayList myArrB = new ArrayList();
this.listBox1.SelectionMode = SelectionMode.MultiExtended;
this.listBox2.SelectionMode = SelectionMode.MultiExtended;
for (int i = 0;i<10;i++) myArr1.Add(i.ToString());
for (int i = 2;i<6;i++) myArrA.Add(i.ToString());
for (int i = 5;i<8;i++) myArrB.Add(i.ToString());
this.FillListBox(myArr1, myArrA, this.listBox1);
this.FillListBox(myArr1, myArrB, this.listBox2);
}
public void FillListBox(ArrayList alData, ArrayList
alSelectionIds,ListBox lstFill)
{
lstFill.Items.Clear();
for (int i=0; i<alData.Count; i++)lstFill.Items.Add(alData[i]);
for (int i=0; i<alSelectionIds.Count;i++)
lstFill.SetSelected(lstFill.FindStringExact
(alSelectionIds[i].ToString()),true);
}
////

I hope this helps?

Cor
Jul 21 '05 #4
Thanks for ur reply. This is a web project.

The values in the Arraylists are same but I have created
2 separate ListItems for them. The values are different in the arraylists
and i have verified this.
I guess there is some problem with selected property in the
listbox.Previously I was getting similar problem for a drop down list box
control. But by changing my logic to use the selectedIndex property instead
of selected property the problem was solved. But in case of list boxes I have
multiple selections hence I cannot use selectedIndex property. So any other
alternative will be helpful.
Any idea if this is a problem with the list box Web control ?

Thanks
Vipin
"Vipin Kedia" wrote:
Hi
I have written a code for showing the list boxes as selected using a
Listitem and the selected property of the items.
Now I have 2 list boxes in my page. But it shows only the selected values
of the last list box in both the list boxes.
If i reverse the calls to the filllistbox methods it shows the value
selected for the 1st list box in both the listboxes.
I have the code for my method below. Is there a way I can solve this
problem?

The method calls are as follows :-

this.FillListBox(myModel.CustomerLBUserList, myModel.SelectedPRList,
this.lstProofreaders);

this.FillListBox(myModel.CustomerLBUserList, myModel.SelectedRevList,
this.lstReviewers);
Thanks in advance
Vipin Kedia
vi********@hotmail.com

-------------------------------------------------

/// <summary>
/// This is overloaded method.
/// This method fills the data as well as preselects item.
/// Note: This method will throw exception if selection mode is single
and selection arraylist contains multiple items for selection
/// </summary>
/// <param name="alData">The ArrayList of ListItem with which ListBox
needs to be populated</param>
/// <param name="alSelectionIds">The ArrayList of Ids which needs to be
selected</param>
/// <param name="lstFill">The ListBox</param>
public void FillListBox(ArrayList alData, ArrayList alSelectionIds,
ListBox lstFill) {
ListItem liAdd = null;
try {
lstFill.ClearSelection();
lstFill.Items.Clear();
for (int i=0; i<alData.Count; i++) {
liAdd = (ListItem) alData[i];
lstFill.Items.Add(liAdd);
lstFill.Items[i].Selected = false;
if (alSelectionIds.Contains(liAdd.Value)) {
lstFill.Items[i].Selected = true;
}
}
}
catch (Exception ex) {
throw ex;
}
finally {
liAdd = null;
}
}

-------------------------------------------------

Jul 21 '05 #5
Vipin,

Almost the same sample I made for a winform now for a webform
\\\
private void Page_Load(object sender, System.EventArgs e)
{
ArrayList myArr1 = new ArrayList();
ArrayList myArrA = new ArrayList();
ArrayList myArrB = new ArrayList();
ListBox1.SelectionMode = ListSelectionMode.Multiple;
ListBox2.SelectionMode = ListSelectionMode.Multiple;
for (int i = 0;i<10;i++) myArr1.Add(i.ToString());
for (int i = 2;i<6;i++) myArrA.Add(i.ToString());
for (int i = 5;i<8;i++) myArrB.Add(i.ToString());
this.FillListBox(myArr1, myArrA, this.ListBox1);
this.FillListBox(myArr1, myArrB, this.ListBox2);
}
public void FillListBox(ArrayList alData, ArrayList
alSelectionIds,ListBox lstFill)
{
lstFill.Items.Clear();
for (int i=0; i<alData.Count; i++)lstFill.Items.Add(alData[i].ToString());
for (int i=0; i<lstFill.Items.Count;i++){
if (alSelectionIds.Contains(lstFill.Items[i].ToString()))
lstFill.Items[i].Selected = true;}
}
//
I hope this helps?

Cor
Jul 21 '05 #6
Thanks Cor. Even this code did not help. Is there an alternative for me to
show the listboxes selected if this is a bug in asp dotnet.

"Cor Ligthert" wrote:
Vipin,

Almost the same sample I made for a winform now for a webform
\\\
private void Page_Load(object sender, System.EventArgs e)
{
ArrayList myArr1 = new ArrayList();
ArrayList myArrA = new ArrayList();
ArrayList myArrB = new ArrayList();
ListBox1.SelectionMode = ListSelectionMode.Multiple;
ListBox2.SelectionMode = ListSelectionMode.Multiple;
for (int i = 0;i<10;i++) myArr1.Add(i.ToString());
for (int i = 2;i<6;i++) myArrA.Add(i.ToString());
for (int i = 5;i<8;i++) myArrB.Add(i.ToString());
this.FillListBox(myArr1, myArrA, this.ListBox1);
this.FillListBox(myArr1, myArrB, this.ListBox2);
}
public void FillListBox(ArrayList alData, ArrayList
alSelectionIds,ListBox lstFill)
{
lstFill.Items.Clear();
for (int i=0; i<alData.Count; i++)lstFill.Items.Add(alData[i].ToString());
for (int i=0; i<lstFill.Items.Count;i++){
if (alSelectionIds.Contains(lstFill.Items[i].ToString()))
lstFill.Items[i].Selected = true;}
}
//
I hope this helps?

Cor

Jul 21 '05 #7
Vipin,

I don't understand why it works with me and not for you, can you tell me
more about that?

Cor
Jul 21 '05 #8
Hi Cor,
I could resolve this issue by cloning the array list customerLBUserList
so that both the list items are referencing different locations.
Thanks for all ur help. When I had used your code the debugger was not
reaching the .selected property only. I do not know why.
I am happy because now we can release our project by this Friday.

Thanx
Vipin

"Cor Ligthert" wrote:
Vipin,

I don't understand why it works with me and not for you, can you tell me
more about that?

Cor

Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DD | last post by:
I have a mainform with a subform. > The main form has a dropdown box "chooseMonth", in the afterupdate event > i requery the subform so all records with the same date are viewed. > Now i only want...
2
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
1
by: Matthew Louden | last post by:
I created a listbox with multiple selectionmode property, but I have trouble to get a list of selected values. The following only gets the first selected value, not others. TextBox2.Text =...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
5
by: Paul | last post by:
Hi I have a listbox and have the selection mode set to multiple in the property box. I am trying to set multiple selected values in code but it only seems to use the last one....
3
by: David L Wright II | last post by:
I have a data bound listbox that I want to select multiple lines then process each line. I bound a two column datatable to the listbox and set the DisplayMember property to the appropriate column...
8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
3
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.