I have a datatable/source binded to a drop down list in ASP.NET C#.
I am trying to return the selected drop down list value using:
dropdownlist.Text
However, this just returns the first list value. How can i get it to return the selected drop down list value?
The dropdownlist.Text property should work.
Make sure that you are not binding the list again on Page_Load. This would reset the SelectedValue to the first value.
Use code like
if(!IsPostBack)
{
//DataBind dropdownlist
}
Use DropDownList.SelectedValue property instead.
Dropdown can return only one value.
For list either you need to customize the dropdown or you need to find third party control to do that.
if You want to do it with 3rd party controls. you can try this
if you want to customize dropdown then view these
1
2
Related
I have an ASP.Net web for that inserts values into a database, I want to reset the selected items in the dropdownList, how do I do it properly?? I know with textBoxes I can use TextBox1.Text = String.empty but it doesnt seem to work with the dropdownLists
If you want to clear all Items means you can use
aspDrop.Items.Clear(); // whill clear all the list items
If you want to change the selected index means you have to use
aspDrop.SelectedIndex = someIndex; // where index must be in the list
Or even you can use .Insert() method to insert an item to a particular index and make it as selected item.
For DropDownList it would be better to first add an empty string at first position of the DropDownList by using yourDropDownList.Items.Insert method and then select it. Something like this:
yourDropDownList.Items.Insert(0, new ListItem(string.Empty, string.Empty));
yourDropDownList.SelectedIndex = 0;
Well if you want to clear dropdownList fully so you can clear it's items source such in this way:
ddItems.Clear();
But if you only want to clear selected dropdownListItem than S.Akbari solution:
ddItems.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ddItems.SelectedIndex = 0;
I guess, everyone has given answers. But remember one thing to do as follows:
AppendDataBoundItems="False"
Set it to false as this will cause the rebound data to be appended to the existing list which will not be cleared prior to binding. The above is applicable when data is bound to the DropDownList. The rest will be done clearing it.
I have a ComboBox whose items I have populated from a database. I wish to know if the selected item has changed from when I initially displayed the ComboBox. How can I determine this?
it is not simple- try to handle EndEdit event/ But you couldnot handle select change in dropdown list
You could try storing the default value in the .Tag property and comparing the two whenever you need.
I'm adding ListItems to a ListBox from two controls, both are DropDownLists.
The ListItem has the properties ListItem.SelectedItem and ListItem.SelectedValue, but I also want the ListBox to keep track of which DropDownList the ListItem came from.
What would be the best way to do this?
You could set the ID of the dropdown programatically as a attribute of the listitem.
ListItem i = new ListItem();
i.Attributes["IDofDropDown"] = "SomeID";
Encode the .Value section so that its something like DropDownList1:SomeValue and then you can use String.Split to extract the info back out of .SelectedValue?
ListItem item contains Text and Value properties that mapped from option element in HTML. So you can not add any other value to this structure. But maybe you can save the source dropdown's id to the Value of the ListItem.
You could create an in memory dataset to store all the data needed and then bind the dataset to your ListBox. When ever a value is selected you will have access to more fields in code behind.
The way I went about solving this, was creating an ArrayList, with another array inside for each item.
I Have a page where there are two Dropdownlist and both have the same data in it but when user selects the value in Dropdownlist1 ,same value should be selected automatically in Dropdownlist2?
Thanks
Smartdev
You can do that a couple of ways. If you're using the built in asp.net postback approach then you can set the value of the dropdown box server side.
If you're wanting to do this on the front end you can use jQuery (or standard Javascript) to set the selectedIndex of the second dropdown.
I assume that's the kind of answer you are after and not just for someone to write the code for you. :)
so in your code behind OnSelectedIndexChanged, you need to rebind both DropDownList1 and DropDownList2 to the same datasource.
Is there a quick way to add a blank value to a drop down list for a winforms application? Some of my dropdowns are bound to lists of objects and some are bound to datarows from a datatable. Since I am setting the datasource property, I can't just add one through code. Is there any quick way of getting a blank value added or will I have to do it manually for each drop down?
You could add a DataRow with empty values to your datatable programatically,
Here's one I've used to do a blank entry before enum values. Obviously this won't work for more complex binding scenarios.
combo1.DataSource = (new object[] { null }
.Concat(Enum.GetValues(typeof(myEnumType)).Cast<object>()))
.ToList();