ASP.Net Dropdownlist ListItem (Enabled=false) not showing in page - c#

I have a dropdownlist in aspx called ddlService.
I want to added the listitems from behind.
When I add, I will create them in order of Title and items underneath like..
Title1
Item1
Item2
Title2
Item1
Item2
Titles should not be able to click. Only Items should be able to click.
ListItem tempServicesItem = new ListItem();
tempServicesItem.Text = tempTitle;
tempServicesItem.Value = tempTitle;
tempServicesItem.Enabled = false;
ddlServices.Items.Add(tempServicesItem);
tempServicesItem = new ListItem();
tempServicesItem.Text = tempItem;
tempServicesItem.Value = tempItem;
ddlServices.Items.Add(tempServicesItem);
The problem I encountered is The ListItems with (Enabled=false) are not appearing in aspx.
When I change it to (Enabled=true), it is appearing.
I must have missed out something. Can anyone point out?
Thanks.

I believe this is what you're looking for. (Not tested)
tempServicesItem.Attributes.Add("disabled", "disabled");

MSDN documentation says
You cannot use this property to disable a ListItem control in a
DropDownList control or ListBox control.
I think you need to set the "disabled" attribute which corresponds to the HTML markup for the option element
tempServicesItem.Attributes["disabled"] = "true";

Related

Obtain selected value from dropdown in code behind

I have the follwing dropdown list and to get the selected value with javascript is easy enough.
<select id="FirstDropDown" runat="server" onchange="ValidatePrimaryDropDown();" >
<option>[Please Select Yes Or No]</option>
<option>Yes</option>
<option>No</option>
</select>
var e = document.getElementById("FirstDropDown");
var dropDownFirst = e.options[e.selectedIndex].value;
I prefer to use this dropdown as apposed to 'asp:DropDownList'.
How can I retrieve the selected value in code behind C#?
There are FindByText and FindByValue function available.
ListItem li = Select1.Items.FindByText("Three");
ListItem li = Select1.Items.FindByValue("3");
li.Selected = true;
Link to source
Is that an aspx page? If so, add a name attribute to your select and use
Requets.Form["elementName"];
in aspx.cs.
Btw: to your javascript code: is there any particular reason why are you using DOM selection instead of jquery? In jquery you wold just use
var selectedItem = $("#FirstDropDown").find(":selected").text()

How can I hide an option in my drop down without changing the query

I have a drop down in aspx that is binded using a class that has a function that retrieves a list of departments. There is a department Name "Audits and Verifying". I do not want to modify the query to do a select Name, DepatmentValueName from departments where DepartmentValueName <> 'AuditsVerifying' . Instead I want to hide it some how in the code in C# is this possible?
DepartmentsAdmin DepartmentName = new DepartmentsAdmin();
DepartmentType.DataSource = DepartmentName.GetAllDepartments();
DepartmentType.DataTextField = "Name";
DepartmentType.DataValueField = "DepartmentValueName";
DepartmentType.DataBind();
Thank you in advance
Try this-
ListItem removeListItem = DepartmentType.Items.FindByText("Audits and Verifying");
DepartmentType.Items.Remove(removeListItem);
You can remove an item from your dropdownlist control after you bind to it.
DepartmentType.Items.Remove(DepartmentType.Items.FindByText("AuditsVerifying"));
The dropdown.Items is an instance of a ListItemCollection. Below is an example of how to remove a ListItem from a ListItemCollection
ListItem myListItem = new ListItem(Delete.Text.ToLower(),Delete.Text.ToLower());
// Check whether the 'ListItem' is present in the 'ListBox' or not.
if(ItemCollection.Contains(myListItem))
{
String deleteString=Delete.Text;
// Delete the listitem entered by the user in textfield.
ItemCollection.Remove(deleteString.ToLower());
Message.Text="<font color='green'><b>Deleted Successfully</b></font>";
}
else
{
Message.Text="<font color='red'><b>No ListItem with the given name is present in the ListBox for deletion.</b></font>";
}
Or you may remove an item based on the Text it displays by using:
DropDownList.Items.Remove("<your string that you want to remove>");
ListItemCollection.Remove() Method

ListItem's value gets replaced by the text on DataBind

I'm at a bit of a loss on this one and haven't been able to find anything helpful in my searches so I'm hopeful someone can help me out here.
I've got a RadioButtonList that I'm adding a List of dynamically created ListItems, where I set both the text and the value for each item. On DataBind for the RadioButtonList the Value for the ListItem gets replaced by the Text, which just doesn't seem to make sense to me.
I can see on the client side when I look in Firebug that the label and the value on the input are the same, and the value is nowhere to be seen.
Has anyone else had any experiences like this, or does anyone know where I might be going wrong?
var rbList = new List<ListItem>();
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
rbList.Add(li);
}
radioButtonList.DataSource = rbList;
radioButtonList.DataBind();
Should you be using Databinding here? Can you not just add your ListItems to the radio button list directly?
I would imagine that the Databinding is getting confused about how to bind your list so is just using ToString on each of your elements which seems to just return the Text Property. This is then being used as both the Text and the Value.
You probably just want to create your items and add them straight to your Radio button control as follows:
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
radioButtonList.Items.Add(li);
}
For those, whose are still struggling - do not forget to fill DataValueField, if control has it. Simply provide string name of your value property and you will be fine.
As Chris mentioned - Databinding was in fact confused and therefore DataValueField exists. My problem was with basic asp:DropDownList.

How can I populate a drop down list control Collection?

How can I populate a drop down list control (ASP.NET) from a SPListTemplateCollection (SharePoint 2007) ?
SPWeb web = SPContext.Current.Web;
ddlTemplateList = new DropDownList();
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataBind();
This code doesn't work in a right way... The name of the list template is not showed.
You need to specify DataTextField and DataValueField to make it work.
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataTextField = "DisplayColumnName";
ddlTemplateList.DataValueField = "ValudColumnName";
ddlTemplateList.DataBind();
use DisplayMember and ValueMember property!
update
DisplayMember and ValueMember are property for WinForm controls.
For asp.net correct solution, as metioned by Muhammad, is to use DataTextField and DataValueField.
Try this one
List<SPWeb> lstSPWeb = web.ListTemplates
ddlTemplateList.DataSource = lstSPWeb;
ddlTemplateList.DataBind();
foreach (SPListTemplate lt in SPContext.Current.Web.ListTemplates)
ddlTemplateList.Items.Add(new ListItem(lt.Name, lt.Type.ToString()));

asp.net: How can I remove an item from a dropdownlist?

I have a dropdownlist, and in some cases need to remove an item (in the code-behind). I need to remove the item based on the value of the item.
How can I do this?
You can use this:
myDropDown.Items.Remove(myDropDown.Items.FindByValue("TextToFind"));
Code:
ListItem removeItem= myDropDown.Items.FindByValue("TextToFind");
drpCategory.Items.Remove(removeItem);
Replace "TextToFind" with the item you want to remove.
myDropDown.Items.Remove(myDropDown.Items.FindByText("TextToFind"))
You can use the
myDropDown.Items.Remove(ListItem li);
or
myDropDown.Items.RemoveAt(int index);
to remove it using C#.
There is also a slightly simpler way of removing the value.
mydropdownid.Items.Remove("Chicago");
<dropdown id=mydropdown .....>
values
Florida
Texas
Utah
Chicago
As other people have answered, you need to do;
myDropDown.Items.Remove(ListItem li);
but if you want the page to refresh asynchronously, the dropdown needs to be inside an asp:UpdatePanel
after you do the Remove call, you need to call:
yourPanel.Update();
myDropDown.Items.Remove(myDropDown.Items.FindByText("Chicago"));
I have Done Like this, i have remove all items except the value coming as 1 and 3.
ListItemCollection liCol = ddlcustomertype.Items;
for (int i = 0; i < liCol.Count;i++ )
{
ListItem li = liCol[i];
if (li.Value != "1" || li.Value != "3")
ddlcustomertype.Items.Remove(li);
}
Try this code.
If you can add any item and set value in dropdown then try it.
dropdown1.Items.Insert(0, new ListItem("---All---", "0"));
You can Removed Item in dropdown then try it.
ListItem removeItem = dropdown1.Items.FindByText("--Please Select--");
dropdown1.Items.Remove(removeItem);
I would add an identifying Id or class to the dropbox and remove using Javascript.
The article here should help.
D
to insert into DropDownList:
DropDownList.Items.Insert(0, new ListItem("-- Select item --", "0"));
and to remove item from DropDownList:
DropDownList.Items.Remove(new ListItem("-- Select item --", "0"));

Categories