List Box Problem
-
Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)
-
Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)
Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)
The SelectedItems property will return the list of all the seleted items in a list box use this property to get the seletecd values. This will return you a string array use foreach loop to get the values. With Regards Yogesh Agarwal
-
Hi all, I m having problem with selecting multiple values in a listbox and inserting tht in the database. I wrote this code: if (lstResource.SelectedIndex != -1) { for (int i = 0; i < lstResource.Items.Count; i++) { if (lstResource.Items[i].Selected == true) { stringBuilder.Append(lstResource.Items[i].Value + ","); } } } but somehow its not giving any value as TRUE, even if it is selected. Thanks in advance :^)
ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.
Cheers, Vikram.
If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.
-
Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Hi Christian Graus, Data is comin from the database table. I did binding on page load. as in below: lstResource.DataSource = ds; //ds-Dataset lstResource.DataTextField = "Names"; On Submit button click i want the selected values to get inserted in database together.
-
ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.
Cheers, Vikram.
If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.
Hi Vikram.. It does have the property called "Selected"
-
Where does the data for the listbox come from ? Are you resetting the data on postback ? If you are, then that will cause all your selections to be lost.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Oh..ok... Thanx Christian Graus .... Actually i was resetting data on postback.. I just put the code if (!IsPostBack) {} And it worked like a charm.... :-D Thanks a lot
-
ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.
Cheers, Vikram.
If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.
Vikram A Punathambekar wrote:
It does not contain a property called Selected.
It's a cross post. Looks like OP is using ASP.NET.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
ListBox.Items[i] returns the object the listbox contains. It does not contain a property called Selected.
Cheers, Vikram.
If the radiance of a thousand suns Were to burst at once into the sky That would be like the splendor of the Mighty one— I am become Death, The shatterer of Worlds.
Hi, Just put a dot after thsi and you get selected as a property. check out. ListBox.Items[i] ----Naina