C# dropdownlist SelectedIndexChanged在按钮单击时触发,如果它们具有相同的值
我有这样一个代码: 在.aspx页面上C# dropdownlist SelectedIndexChanged在按钮单击时触发,如果它们具有相同的值,c#,asp.net,C#,Asp.net,我有这样一个代码: 在.aspx页面上 <asp:ListItem Value="1"> 1</asp:ListItem> <asp:ListItem Value="2"> 2</asp:ListItem> <asp:ListItem Value="2" Selected="True"> 3</asp:ListItem> <asp:ListItem Value="4"> 1</asp
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="2" Selected="True"> 3</asp:ListItem>
<asp:ListItem Value="4"> 1</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
这里的问题是,当我点击按钮时,DropDownList1\u SelectedIndexChanged也会触发。
当我浏览时,发现这种情况的发生是由于不同itemtext的值相同
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="2" Selected="True"> 3</asp:ListItem>
2
3.
我不能改变价值
运气好吗?或者问题到底是什么
谢谢
Vijay请检查DropDownList AutoPostBack属性 如果AutoPostBack为真 获取或设置一个值,该值指示是否向服务器回发 当用户更改列表选择时自动发生
确保在网页上启用ViewState,并在页面加载时添加它
if (!IsPostBack)
{
//do code here
}
SelectedIndexChanged
在选择更改时激发。它不考虑相同的值或文本
这就是为什么它被命名为
SelectedIndexChanged
&而不是selectedValueChanged或SelectedTextChanged
因此,如果您希望您的
SelectedIndexChanged
事件,并且希望它在具有相同的值或文本时不会触发。这是不可能的
IMHO:值
或文本
不应在下拉列表中重复。您应该在绑定数据源之前清除数据源中的重复值。SelectedIndexChanged
在选择更改时激发。它不考虑相同的值或文本。这将使SelectedIndexChanged
事件根本不会触发。我不知道在这种情况下启用ViewState
或使用IsPostBack
有何用处。但仍有投票权。
if (!IsPostBack)
{
//do code here
}