By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

.Selected property for listbox does not work

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.