asp.net的下拉列表验证 [英] drop down list validation for asp.net

查看:71
本文介绍了asp.net的下拉列表验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户忘记在下拉列表中选择一个值,因此如果他们忘记选择,我需要给出警报消息,怎么办?我不知道请帮助我

in vb.net web project, user forget to select a value in dropdownlist, so i need to give alert message if they forget to select, how to do? i don''t know help me please

推荐答案

您好,您可以实现一个简单的JavaScript验证功能,并在Button的OnClientClick方法上调用它.
下面是代码.

aspx代码
Hi, you can implement a simple JavaScript validation function and call it on Button''s OnClientClick method.
Below is the code.

aspx code
<asp:DropDownList ID="dropDown1" runat="server" AutoPostBack="false">
    <asp:ListItem Text="None" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text3" />
</asp:DropDownList>

<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="javascript:return ValidateDropDown();" />



JavaScript代码



JavaScript Code

function ValidateDropDown() {
    var cmbID = "<%=dropDown1.ClientID %>";
    if (document.getElementById(cmbID).selectedIndex == 0) {
        alert("Please select a Value");
        return false;
    }
    return true;
}



希望这能解决您的问题.



Hope this will solve your problem..


<asp:DropDownList ID="dropDown1" runat="server" AutoPostBack="false">
    <asp:ListItem Text="None" selected="True"/>
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text3" />
</asp:DropDownList>

<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" text="*" controltovalidate="dropDown1" errormessage="Please Select Category" initialvalue="None" xmlns:asp="#unknown"></asp:requiredfieldvalidator>


<asp:Button ID="Button1" runat="server" Text="Submit"  />


这篇关于asp.net的下拉列表验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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