ASP.NET & C# - Two values in a listbox? - c#

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.

Related

How to Remove the selected index of a DropDownList?

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.

ASP.NET C# DataTable DropDownList Issue

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

Get selected user choice from a list of DropDownLists displayed via a Repeater

Right now I have a List<> of Dictionary elements that I display to a webpage. I use a Repeater for the entire List<> because the List<> length is arbitrary, and DropDownList for each Dictionary element. The user sees a Repeating list of DropDownLists and selects one Dictionary element from each of these said DropDownLists as their choice.
I would like to retrieve the Dictionary key of whatever the user's said choice is in each DropDownList. If this step is easier done afterwards, just retrieving everything is fine too, but my end goal is to isolate said keys and put them into an array. Right now I bind the List<> directly to the Repeater and everything displays fine, but I have no idea what to put into the function that is called when they press the final Submit button.
Cheers~
You can loop through each item in the repeater and then find the child control:
foreach (RepeaterItem ri in myRepeater.Items)
{
DropDownList dropDownList = (DropDownList)ri.FindControl("dropDownList");
string myValue = dropDownList.SelectedValue;
}

How to get elements from an object in C#?

I am using the AutoCompleteBox in WPF, I populate the suggestions with a List that consists of four fields. When the user selects an item and I reach my eventHandler, i can see that
MyAutoCompleteBox.SelectedItem
is an object that has my four values, if i hover this text in the debugger i can see the four values listed, however i don't know how to access these values in the code.
I tried
List<Codes> selected = MyAutoCompleteBox.SelectedItem as List<Codes>;
where Codes is my List. selected returns as null and empty every time. Is there a way to get to these values? Thanks!
If you want the listing of items used as the backing collection for the AutoCompleteBox try...AutoCompleteBox.ItemsSource.
Can you try:
Codes selected = MyAutoCompleteBox.SelectedItem as Codes;
or
Codes[] selected = MyAutoCompleteBox.SelectedItem as Codes[];
It means that you cannot convert whatever MyAutoCompleteBox.SelectedItem is to a List.

Dropdownlist with datasource set and empty value?

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();

Categories