防止在ASP.NET中选择默认值时下拉的“选定索引更改”事件
Prevent the Selected Index Changed event of dropdown on selecting the default value in ASP.NET
我有一个值
的下拉菜单<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>SELECT</asp:ListItem>
<asp:ListItem>AUS</asp:ListItem>
<asp:ListItem>BAN</asp:ListItem>
<asp:ListItem>CAN</asp:ListItem>
<asp:ListItem>DEN</asp:ListItem>
<asp:ListItem>ENG</asp:ListItem>
<asp:ListItem>IND</asp:ListItem>
</asp:DropDownList>
所选索引更改我将项目名称写为:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(DropDownList1.SelectedItem);
}
我想当我将选择默认值是选择,事件不应该被调用。只有当我选择除了select以外的任何内容时,才应该调用它。
您可以根据所选择的选项自行PostBack到服务器。
注意:
不要设置DropDownList的AutoPostBack="True"
<asp:DropDownList runat="server" ID="DropDownList1"
onChange="changeDropDownList();"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="" Text="SELECT" />
<asp:ListItem Value="AUS" Text="AUS" />
<asp:ListItem Value="BAN" Text="BAN" />
<asp:ListItem Value="CAN" Text="CAN" />
<asp:ListItem Value="DEN" Text="DEN" />
<asp:ListItem Value="ENG" Text="ENG" />
<asp:ListItem Value="IND" Text="IND" />
</asp:DropDownList>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function changeDropDownList() {
// If you want to check by selected text, use this line.
// var text= $('#<%= DropDownList1.ClientID %> option:selected').text();
var value = $('#<%= DropDownList1.ClientID %> option:selected').val();
if (value != "") {
__doPostBack('<%= DropDownList1.ClientID %>', '');
} else {
return false;
}
}
</script>
最简单的方法之一是使用如下所示的必填字段验证器
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="" InitialValue="SELECT" >
</asp:RequiredFieldValidator>
当你输入InitialValue="SELECT"
时它不允许你对SELECT值
相关文章:
- 触发具有较低Z索引值的DOM元素的事件
- 语义 UI 手风琴获取打开事件中打开的项目的索引
- KendoUI:如何获取点击的图表元素的索引'事件中的项
- 将事件添加到涉及for()的索引中的多个子节点
- GridView页面索引更改事件导致JavaScript运行时错误
- 在JavaScript中更改下拉列表索引后,ASP.Net提交按钮单击事件不起作用
- 使用 jQuery 创建动态元素 单击事件始终返回 for 循环中的最后一个索引
- Jquery 复制了具有特定索引的 html 类,无法识别点击事件
- 使用 javascript 更改 Gridview 文本框的可见属性 true 在下拉列表的选定索引更改事件上为 tru
- 如何在鼠标悬停事件时添加索引
- 在 Firefox 中,当选择具有位置和 z 索引时,为什么更改事件不会在选项卡上触发
- 在事件触发后获取 jQuery 元素的索引
- 在事件侦听器中获取 e.target 子项的索引
- 下拉列表选定的索引更改事件
- 将每个事件与正则表达式匹配,并在字符串中获取它们的索引
- 正在将索引值绑定到事件处理程序
- 如何根据索引中控件的onchange事件设置mvc 5视图中隐藏字段的值
- onhashchange事件不't检测“/主页/索引“;以及“/"-ASP.NET MVC3
- 如何获取数组中所有具有相同值的事件的索引
- Z索引并单击事件