Assume that I have a dropdownlist with 2 items and, by default, the first item is selected. If I select the click the first item in the dropdownlist, is there a way I can get the SelectedIndexChanged() event to still fire?
I thought I could do it by setting the SelectedIndex of the dropdownlist to -1, but that didn't work, because it does not display the currently selected value, so it is misleading.
An issue I have on this is that the dropdownlist is used for sorting. I have the sorting semi-working in that if I select the second item, it will sort in ascending order for example, but if I want to sort in descending order now using the second item, I have to select another item and then go back to the second item.
Even if I add a Select By, I think the best solution to sorting is to just have more items in the dropdownlist like:
Sort Numbers (Asc)
Sort Numbers (Desc)
Sort Alphabet (Asc)
Sort Alphabet (Desc)
Unfortunately no: the event will only fire if the user changes the selection from one item to another.
You might consider adding an item with the text "Please select..." to the top of your list.
Just out of curiosity, is your first item supposed to be a selectable item or is it something like, "select something below"? Because you can actually set the text value of your dropdown to the aforementioned quote, and it won't be a selectable item, so no matter what they choose, selectedindexchanged will always fire initially.
Otherwise you'll have to do something like this:
DropDownList1.Items.Insert(0, "Select an Item")
DropDownList1.SelectedIndex = 0
After you've bound your control.
Edited to add
I actually do this with my dropdowns:
var a = new AddressesBLL();
cmbPersonAddress1.DataSource = a.GetAddresses();
cmbPersonAddress1.DataBind();
//Set the default text to the below text but don't let it be part of the selections on the drop down.
cmbPersonAddress1.Text = "Please select an existing address...";
Which doesn't make "Please select an existing address..." a selectable item. When you expose the ddl, the first selectable item is the first address, so the selectedindexchanged will always fire.
Like said before, add a first item of text to direct the user to select an item from the list.
If you are databinding items, you want to insert the item afterwards.
Dropdownlist1.datasource = whatever
Dropdownlist1.datatextfield = "Something"
dropdownlist1.datavaluefield = "ValueField"
dropdownlist1.databind
dropdownlist1.items.insert(0, "----Select Something!----")
dropdownlist1.selectedindex = 0
and then in your SelectedIndexChanged
event you can prevent action on the first item by wrapping all of your code in an if statement:
If DropDownList1.SelectedIndex <> 0 then
Do Your Work
End If
Note: This is based on the updated content of the question.
Let's say you have one drop down list and one listbox (dropdownlist1 and listbox1)
You can set up your initial drop down list in your page_load event as such:
dropdownlist1.items.insert(0, "----Select Sort Method----")
dropdownlist1.items.insert(1, new ListItem("Alphabetic Ascending", "AlphaAsc"))
dropdownlist1.items.insert(2, new ListItem("Alphabetic Descending", "AlphaDesc"))
dropdownlist1.items.insert(3, new ListItem("Numeric Ascending", "NumAsc"))
dropdownlist1.items.insert(4, new ListItem("Numeric Descending", "NumDesc"))
dropdownlist1.selectedindex = 0
Then on your dropdownlist1.selectedindexchanged event you would handle it as such:
if dropdownlist1.selectedindex <> 0 then
select case dropdownlist1.selectedvalue
case "AlphaAsc"
Insert Code to Sort ListBox1 Alphabetically in ascending order
case "AlphaDesc"
Insert Code to sort ListBox1 Alphabetically in descending order
case "NumAsc"
Insert code to sort ListBox1 Numerically in ascending order
case "NumDesc"
Insert code to sort ListBox1 Numerically in descending order
end select
end if
Note: You would want to make sure that your dropdownlist1's AutoPostBack property is set to true if you want the sorting to happen immediately upon selection of an item.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.