如何在不使用javascript和asp.net选择dropdownlist值的情况下显示验证

how can i show validation on without selecting dropdownlist value using javascript and asp.net

本文关键字:dropdownlist 选择 情况下 验证 显示 net asp javascript      更新时间:2023-09-26

我有dropdownlist和一个文件上传按钮,因为当我没有选择dropdownlistvalue时,我想显示错误消息我只使用java脚本来引发错误消息我试过的代码是:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" lang="javascript">
    $(document).ready(function () {
        $('#txtBoxValue').click(function () {
            if ($("#ddlsubjname").val() > 0) {
                return true;
            }
            else {
                alert('Please select Country')
                return false;
            }
        })
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center"><tr><td>
        <asp:DropDownList ID="ddlsubjname" runat="server" > 
            <asp:ListItem>--Select Items--</asp:ListItem>
            <asp:listitem>Apple</asp:listitem>
            <asp:listitem>>Mango</asp:listitem>
            <asp:listitem>Grapes</asp:listitem>
       </asp:DropDownList>
        </td></tr>
        <tr><td></td></tr>
        <tr>
            <td>
             <span id="txtBoxValue"><asp:FileUpload ID="filuploadmp3" runat="server" /></span>  
            </td>
            <td><asp:Label ID="lblmsg" runat="server" Text=""></asp:Label></td>
        </tr>
    </table>

有人能帮我解决吗

我对你的问题了解多少。。。您可以使用此示例验证您的下拉列表

    <asp:DropDownList ID="ddlFruits" runat="server">
        <asp:ListItem Text="Please Select" Value=""></asp:ListItem>
        <asp:ListItem Text="Mango" Value="1"></asp:ListItem>
        <asp:ListItem Text="Apple" Value="2"></asp:ListItem>
        <asp:ListItem Text="Orange" Value="3"></asp:ListItem>
</asp:DropDownList>
    <asp:Button ID="btnSubmit" Text="Validate" runat="server" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("[id*=btnSubmit]").click(function () {
                var ddlFruits = $("[id*=ddlFruits]");
                if (ddlFruits.val() == "") {
                    //If the "Please Select" option is selected display error.
                    alert("Please select an option!");
                    return false;
                }
                return true;
            });
        });
    </script>