c# - DropDownListItems. Can't set any items to be disabled when bound to a datasource -
i'm using code below grab list of items database , bind them dropdownlist.
i insert separator lines in itemlist , set
seperator.attributes.add("disabled", "true"); seperator.enabled = false;
this should make items non-selectable in dropdown doesn't.
if loop through items after i've bound them, work. surely should able disable items before bind them dropdownlist?
dropdownlist gvcategory = (dropdownlist)gridviewactvities.rows[0].findcontrol("gvcategory"); gvcategory.datasource = formatcategorieslistitems(); gvcategory.databind(); public listitemcollection formatcategorieslistitems() { oracleconnection sqlconn = new oracleconnection(configurationmanager.connectionstrings["connectionstring1"].connectionstring); sqlconn.open(); oraclecommand sqlselect = new oraclecommand(@"select id, taskgroup, taskcategory hott_groups_cats order 'sort'", sqlconn); sqlselect.commandtype = system.data.commandtype.text; oracledataadapter sqladapter = new oracledataadapter(sqlselect); dataset mydataset = new dataset(); sqladapter.fill(mydataset); sqlconn.close(); listitemcollection listitems = new listitemcollection(); //add blank item listitems.add(new listitem("")); string previoustaskgroup = ""; // database items list ready bind dropdown (int = 0; < mydataset.tables[0].rows.count; i++) { string taskgroup= mydataset.tables[0].rows[i]["taskgroup"].tostring(); string taskcategory = mydataset.tables[0].rows[i]["taskcategory"].tostring(); string taskid= mydataset.tables[0].rows[i]["id"].tostring(); // insert seperator row based on taskgroup if (taskgroup != previoustaskgroup) { listitem seperator = new listitem("--" + taskgroup + "--", ""); seperator.attributes.add("disabled", "true"); seperator.enabled = false; listitems.add(seperator); if (i != mydataset.tables[0].rows.count) { previoustaskgroup = mydataset.tables[0].rows[i]["taskgroup"].tostring(); i--; } } else { listitems.add(new listitem(" " + taskcategory, taskid)); if (i != mydataset.tables[0].rows.count) { previoustaskgroup = mydataset.tables[0].rows[i]["taskgroup"].tostring(); } } } return listitems; }
unfortunately case, don't think possible before bind that. here question on subject: make drop down list item unselectable
update:
i'm editing answer based on derrick's answer , comment it. derrick suggesting after dropdownlist bound, go through , disable separators. more this:
protected void gvcategory_databound(object sender, eventargs e) { foreach (listitem li in gvcategory.items) { if(li.text.contains("--")) li.attributes.add("disabled", "true"); } }
update 2:
add ondatabound event manipulate dropdownlist after bound.
<asp:dropdownlist id="gvcategory" runat="server" ondatabound="gvcategory_databound"></asp:dropdownlist>
Comments
Post a Comment