C# DropDownList 选定索引更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18030618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DropDownList Selected Index change
提问by Syed Salman Raza Zaidi
I am using DropDownList inside Updatepanel with its autopost back property set to true, its working fine except when it has SelectedValue=0(ie SelectedIndex=0)
我在 Updatepanel 中使用 DropDownList,其 autopost back 属性设置为 true,它工作正常,除非它有 SelectedValue=0(即 SelectedIndex=0)
here's my drop down list
这是我的下拉列表
<asp:UpdatePanel ID="panel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddFilter" EnableViewState="false" runat="server" Style="width: 168px;
border: none;" OnSelectedIndexChanged="ddComapanyFilter_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="All" Value="0"></asp:ListItem>
<asp:ListItem Text="Flagged" Value="1"></asp:ListItem>
<asp:ListItem Text="New" Value="2"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
and this is my SelectedIndexChangedEvent
这是我的 SelectedIndexChangedEvent
protected void ddComapanyFilter_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddComapanyFilter.SelectedValue == "0")//All
{
//code
}
else if (ddComapanyFilter.SelectedValue == "1")//Flagged
{
//code
}
else if (ddComapanyFilter.SelectedValue == "2")//New
{
//code
}
}
When I select New or Flagged it works fine, but when I again select All,it don't do any thing,I have tried debugging it,in case of All its not hitting the code.
当我选择新建或标记时它工作正常,但是当我再次选择全部时,它不做任何事情,我试过调试它,以防万一它没有命中代码。
I tried by replacing my drop down code with this
我尝试用这个替换我的下拉代码
<asp:DropDownList ID="ddFilter" EnableViewState="false" runat="server" Style="width: 168px;
border: none;" OnSelectedIndexChanged="ddComapanyFilter_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="All" Value="1"></asp:ListItem>
<asp:ListItem Text="Flagged" Value="2" Selected="True"></asp:ListItem>
<asp:ListItem Text="New" Value="3"></asp:ListItem>
</asp:DropDownList>
Now When I am selecting Flagged,after selecting New or All, its not hitting
现在,当我选择已标记时,在选择新建或全部后,它没有命中
采纳答案by Ahmed Alaa El-Din
This is a known issue in dropdownlist, always first list item doesn't fire that's why you can add a dummy listitem at the beginning like "Select.." or what ever you want to make it work
这是下拉列表中的一个已知问题,始终不会触发第一个列表项,这就是为什么您可以在开头添加一个虚拟列表项,例如“选择..”或您想让它工作的任何内容
<asp:UpdatePanel ID="panel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddFilter" EnableViewState="false" runat="server" Style="width: 168px;
border: none;" OnSelectedIndexChanged="ddComapanyFilter_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="whatever" value=""></asp:ListItem>
<asp:ListItem Text="All" Value="0"></asp:ListItem>
<asp:ListItem Text="Flagged" Value="1"></asp:ListItem>
<asp:ListItem Text="New" Value="2"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>