Hi,
Top of the hour to you all. I have got this little problem in asp.net c#. I have got a ListBox which some listItem like
Mango
Orange
Banana
I want to achieve this, when a user clicks on a list item for example click on 'Mango' the mango ListItem should only be disabled in the ListBox ....
Thanks in advance
Loading

VulpesRank #55 · 29.2K reputationPosted Apr 28, 2011, 5:03 AM EDT
Giwa Abdulhakeem25 reputationPosted Apr 28, 2011, 5:07 AM EDT
Would really appreciate a snippet on that though.
Cheers
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 28, 2011, 5:11 AM EDT
You Can disable the items in listbox , i took one listbox and a button on page. Button is used to retrive items which are disabled in list.
try this :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listBox1.Items.Add("Item1");
listBox1.Items.Add("Item2");
listBox1.Items.Add("Item3");
}
}
protected void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.SelectedItem.Enabled = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count ; i++)
{
listBox1.Items[i].Enabled = true;
}
}
VulpesRank #55 · 29.2K reputationPosted Apr 28, 2011, 5:35 AM EDT
Unfortunately, Shrikant's code doesn't work because as it says in the above link:
"You cannot use this property to disable a ListItem control in a DropDownList control or ListBox control."
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 28, 2011, 6:14 AM EDT
I think you miss understood some whr, my code will work for Items which are in ListBox control and in MSDN link , they are talking about ListItem for RadioButtonList control or a CheckBoxList control.
and above code i have implemented & it works for me.
Thanks
VulpesRank #55 · 29.2K reputationPosted Apr 28, 2011, 6:22 AM EDT
On the other hand, when you set Enabled to false for items in the CheckBoxList and RadioButtonList it does gray them out i.e. what you'd normally understand by disabling them.
It may be, of course, that Giwa doesn't mind temporarily removing the items from the ListBox and, if so, then I think your code is the best solution for him.
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 28, 2011, 6:44 AM EDT
listBox1.Items.RemoveAt(listBox1.SelectedIndex);