.Selected property for listbox does not work

G

Guest

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
(e-mail address removed)

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

/// <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;
lstFill.Items.Add(liAdd);
lstFill.Items.Selected = false;
if (alSelectionIds.Contains(liAdd.Value)) {
lstFill.Items.Selected = true;
}
}


}
catch (Exception ex) {
throw ex;
}
finally {
liAdd = null;
}
}

-------------------------------------------------
 
C

Cor Ligthert

Vipin,

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

Cor
 
G

Guest

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
 
C

Cor Ligthert

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);
for (int i=0; i<alSelectionIds.Count;i++)
lstFill.SetSelected(lstFill.FindStringExact
(alSelectionIds.ToString()),true);
}
////

I hope this helps?

Cor
 
G

Guest

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
 
C

Cor Ligthert

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.ToString());
for (int i=0; i<lstFill.Items.Count;i++){
if (alSelectionIds.Contains(lstFill.Items.ToString()))
lstFill.Items.Selected = true;}
}
//
I hope this helps?

Cor
 
G

Guest

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.
 
C

Cor Ligthert

Vipin,

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

Cor
 
G

Guest

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
 

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

Top