ASP.NET - Disable DropDown Item (group name) - Asked By shekhar kumar on 08-Oct-10 09:21 AM

Hi,

I have a dropdown list on my page. I have written a code to diable some of the dropdown items on the basis of some condition. It is working fine on every browser except IE6. 
I don't want to use custom contols.
Can it be done on IE 6 also?

Code for diable items are as follows:  

foreach (ListItem li in ddlCategory.Items)
                {
                    if (li.Value.ToString().Split(';').Length == 2)
                    {
                        if (li.Value.ToString().Split(';')[1].ToString() == "P")
                        {
                            //li.Attributes.Add("style", "font-weight:bolder");
                            li.Attributes.Add("disabled", "true");
                            li.Attributes.Add("class", "option");

                        }
                    }
                }

Thanks

Kirtan Patel replied to shekhar kumar on 08-Oct-10 09:28 AM
you can use Dropdown Items Enable Property also to perform same task



 protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      int x = 10;
      if (x == 10)
      { 
           /* Disable Item 2-3 */
        DropDownList1.Items[1].Enabled = false;
        DropDownList1.Items[2].Enabled = false;

      }
    }
  }
shekhar kumar replied to Kirtan Patel on 08-Oct-10 09:42 AM
Thanks for your quick response. But when i am setting this propery the item is not showing in the dropdown list.
I am using asp.net 4.0.

Thanks
Kirtan Patel replied to shekhar kumar on 08-Oct-10 09:48 AM
if you just want to disable it then why you want to show if user not going to select it ? better ways is not to shown as you dont want user to click it
shekhar kumar replied to Kirtan Patel on 08-Oct-10 09:58 AM
I have a list of category and subcategory. I want to show the list of subcategory after the category appears.
Suppose I have a category Electronic and I have Subcategory TV, Washin Machine ...
then it should appear like this

Electronic (unselectable)
Tv
Washing Machine
...


It is easy to navigate in the list for the end user.
There is option in the html select. But not in the asp.net dropdown list. 
I have tried using the html select control but there to it has no option in that control too.

Thanks.

shekhar kumar replied to Kirtan Patel on 08-Oct-10 10:08 AM
On the home page of this also there is a dropdown which having some group. You can see that.
It has the Name Topic.
:)

Thanks Once again
Rohan Dave replied to shekhar kumar on 09-Oct-10 02:38 AM
You can use OptionGroup (<optgroup>) in asp.net Dropdownlist Control.  <optgroup> is a XHTML standard and has the effect of categorizing the items in <Select> to make your dropdownlist similar to the "All Topics" dropdown in this site.

You also need to use WebControlAdapter class which is new in asp.net 2.0 which allows to override the rendering behaviour of any asp.net controls.

visit below site. It has same example with code which you want..

http://www.codeproject.com/KB/custom-controls/DropDownListOptionGroup.aspx
http://weblogs.asp.net/jeff/archive/2006/12/27/dropdownlist-with-optgroup.aspx

try by this and let us know...