如果在下拉列表中已经选择了Item,则不会触发SelectedIndexChanged事件吗? [英] SelectedIndexChanged event does not fire if Item is already selected in a dropdownlist?

查看:68
本文介绍了如果在下拉列表中已经选择了Item,则不会触发SelectedIndexChanged事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含2个项目的下拉列表,默认情况下,第一个项目被选中.如果选择单击下拉列表中的第一项,是否有办法让SelectedIndexChanged()事件仍然触发?

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?

我以为可以通过将下拉列表的SelectedIndex设置为-1来做到这一点,但这没有用,因为它不显示当前选择的值,因此具有误导性.

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:

排序编号(Asc)

排序编号(Desc)

排序字母(Asc)

排序字母(Desc)

推荐答案

注意:这是基于问题的更新内容.

Note: This is based on the updated content of the question.

假设您有一个下拉列表和一个列表框(dropdownlist1和listbox1)

Let's say you have one drop down list and one listbox (dropdownlist1 and listbox1)

您可以在page_load事件中设置初始下拉列表,如下所示:

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

然后在您的dropdownlist1.selectedindexchanged事件上按如下方式处理它:

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 

注意:如果希望在选择项目后立即进行排序,则要确保将dropdownlist1的AutoPostBack属性设置为true.

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.

这篇关于如果在下拉列表中已经选择了Item,则不会触发SelectedIndexChanged事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆