[英]SelectedIndexChanged event does not fire if Item is already selected in a dropdownlist?
Assume that I have a dropdownlist with 2 items and, by default, the first item is selected.假设我有一个包含 2 个项目的下拉列表,并且默认情况下选择了第一个项目。 If I select the click the first item in the dropdownlist, is there a way I can get the SelectedIndexChanged() event to still fire?
如果我选择单击下拉列表中的第一项,有没有办法让 SelectedIndexChanged() 事件仍然触发?
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.我以为我可以通过将下拉列表的 SelectedIndex 设置为 -1 来实现,但这不起作用,因为它不显示当前选定的值,因此具有误导性。
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:即使我添加了 Select By,我认为排序的最佳解决方案是在下拉列表中添加更多项目,例如:
Sort Numbers (Asc)排序数字 (Asc)
Sort Numbers (Desc)排序编号(降序)
Sort Alphabet (Asc)排序字母 (Asc)
Sort Alphabet (Desc)排序字母 (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.
因为您实际上可以将下拉列表的文本值设置为上述引用,并且它不会是可选项目,因此无论他们选择什么, selectedindexchanged 始终会在最初触发。
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.
当您公开 ddl 时,第一个可选项目是第一个地址,因此 selectedindexchanged 将始终触发。
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:然后在您的
SelectedIndexChanged
事件中,您可以通过将所有代码包装在 if 语句中来阻止对第一项的操作:
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)假设您有一个下拉列表和一个列表框(dropdownlist1 和 listbox1)
You can set up your initial drop down list in your page_load event as such:您可以在 page_load 事件中设置初始下拉列表,如下所示:
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:然后在您的 dropdownlist1.selectedindexchanged 事件中,您可以这样处理它:
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.注意:如果您希望在选择项目后立即进行排序,则需要确保将 dropdownlist1 的 AutoPostBack 属性设置为 true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.