tags:

views:

2499

answers:

4

Assume that I have a dropdown with 2 items and by default, the first item is selected. If I select the click the first item in the dropdown, is there a way I can get the selectedIndexChanged event to still fire?

Can I do it by setting the SelectedIndex of the Dropdown to -1, for example?

Well that didn't work, lol, because it does not display the currently selected value, so it is misleading.

An issue I have on this is that the dropdown 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 dropdown like:

Sort Numbers (Asc)

Sort Numbers (Desc)

Sort Alphabet (Asc)

Sort Alphabet (Desc)

Thanks, XaiSoft

+2  A: 

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.

GregD
Well, right now I have 2 items which are both selectable. The first one is selected by default.
Xaisoft
I haven't tried your code yet, but what I did do was try to set the SelectedIndex to -1 as I mentioned in the Question, but this was just displaying the first item again even after I selected the second.
Xaisoft
I shouldn't have to do this after I bind the control right? At design time, can't I just add another item and if I do, can it be made not to fire the SelectedIndexChange Event.
Xaisoft
I just tried adding the Text, but it didn't show up. I did it in the Page Load.
Xaisoft
+5  A: 

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.

teedyay
If I add Please Select... to the top of the dropdown. Can I prevent that selection from firing the SelectedIndexChanged event.
Xaisoft
@Xaisoft: You can't necessarily prevent it from firing when someone selects the first "Please Selet..." item, but you can use an if statement in your code like "If dropdownlist1.selectedindex <> 0 then" go and do your work.
TheTXI
Ok, why didn't I think of that, lol, I must be stupid today. Thanks.
Xaisoft
+1  A: 

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
TheTXI
Why do you add -----Select Something!----- after you bind instead of at design time for example.
Xaisoft
@Xaisoft: You can add it at design time using the design editor if you want, you will want to make sure though that you have the AppendDataBound property set to True, otherwise when you databind your list it will overwrite whatever you put there during design time.
TheTXI
@Xaisoft: That should have been AppendDataBoundItems as the property name.
TheTXI
ok, I did it dynamically in the Page_Load, but the ----Select Something--- was third in the list. Can you specify that you want it to be the first item.
Xaisoft
ok, sorry, it is probably the insert instead of the add.
Xaisoft
@Xaisoft: Yes, use the .Insert() method instead of .Add(), that way you can tell the control which position you want to insert into.
TheTXI
Read update above and tell me what you think.
Xaisoft
+1  A: 

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.

TheTXI
Ok, this is what I was thinking, but I probably would not need a Select Sort Method, if I just start of with a default.
Xaisoft
If you have an original method you are sorting the ListBox (such as alphabetically in ascending order) you could set that sort method as your default. I am making the assumption by giving a default item in the box that your listbox has no initial sorting on it and you need to provide instruction.
TheTXI