How to Remove the selected index of a DropDownList? - c#

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.

Related

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

How do you select a lower level item as the displaymember for a ListBox

I have a List of FLECk.IWebSocketConnection
I am trying to display this list in a listbox using the IWebSocketConnection.ConnectionInfo.ClientIpAddress property, but I must be doing something wrong
Sockets= new List<IWebSocketConnection>();
ListBox1.DataSource=Sockets;
ListBox1.DisplayMember="ConnectionInfo.ClientIpAddress";
When I add to Sockets using
Sockets.Add(socket);
I have to rebind the datasource
ListBox1.DataSource=Sockets;
ListBox1.DisplayMember="ConnectionInfo.ClientIpAddress";
But the display in the lisbox is "Fleck.WebSocketConnection" as opposed to the expected Ip number.
Just to clarify, The write number of connections are listed. I am simply getting the wrong display information. If I create a label and set its text to
label1.Text =((IWebSocketConnection)SocketList.Items[0]).ConnectionInfo.ClientIpAddress;
it displays the IP number
Can someone tell me what I am doing wrong here.
There may be an alternative approach, but one thing you could do is flatten out the list members you need to display and bind to that.
For example:
var SocketInfos = sockets.Select((s) => new {ClientIpAddress = s.ConnectionInfo.ClientIpAddress,
Socket = s}).ToList();
ListBox1.DataSource=SocketInfos ;
ListBox1.DisplayMember="ClientIpAddress";
You'd need to regenerate the list whenever you change the source list though.
This way you can still get back to the selected Socket Instance from the selected ListBox Item.

ListBox SelectedIndex returning wrong value

So I have an unbound ListBox populated with ListBox items with values such as:
new ListItem("Item1", "1")
new ListItem("Item2", "2")
new ListItem("Item3", "0")
new ListItem("Item4", "0")
new ListItem("Item5", "0")
A delete button should allow a user to delete a ListItem. However, whenever the user tries to delete an item with a value of zero, only the topmost 'zero' item is selected. In the example above, if 'Item5' was selected and delete was clicked, 'Item3' would be given the boot. Items with proper numbered values behave appropriately. Any idea why this might be happening, or did I answer my own question by typing it out (perhaps ASP.net can't differentiate between items with identical values)?
Any help would be greatly appreciated. Many thanks!
Edit: To clarify, items with values of zero are removed differently than items with other values. Ie: If an item has a non-zero value, it is removed by value. If it has a value of zero, it is removed by it's ListBox index, but that index is always being returned as the index directly under the last non-zero value.
You found the solution yourself. If you query the item by value, then the first item with that value will be returned.
Instead of removing by value, remove by the index:
myDropDown.Items.RemoveAt(0);
ListControl.Items.FindByText and ListControl.Items.FindByValue are only going to return a single ListItem, and I assume the first one they find.
If you wanted to handle your situation with multiples of the same value, then you'd likely have to write some loop to find and remove them all.
Your list items should have unique values. Asp.net only posts back the value selected for the list box so the server will be unable to tell which actual item it is so it just grabs the first match.
I would suggest looping through the collection and deleting the items, like this:
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
ListItem item = DropDownList1.Items[i];
if (item.Value == "0")
DropDownList1.Items.RemoveAt(i);
}

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

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.

How do I prevent selectedValue altering when calling tableAdapter's Fill() method?

I have bound my ListBox to some data.
The problem is when I call myTableAdapter.Fill(..) method, SelectedValue changes to whatever is the first item ID in the list. Although "Selected Value" in VS is not bound anywhere (see image).
alt text http://img370.imageshack.us/img370/2548/ss20090108212745qz2.png
How do I prevent this behaviour, please?
Thank you very much for helping.
You shouldn't be binding on each request. If you absolutely have to bind on each request for some reason you have to set SelectedIndex on the ListBox manually. This is because the Fill method first clears the list then creates new list items for the fetched data.
The simplest way I can think of is to change your table adapter fill code to something like this:
string preSelected = myDropDownList.SelectedValue;
myTableAdapter.Fill(myDataTable);
myDropDownList.SelectedValue = preSelected;
You will run into an issue if the item doesn't exist anymore, so you may want to add a condition to check for that.

Categories