使用c#在asp.net中验证多个下拉列表 [英] Validate mutiple dropdownlist in asp.net using c#

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

问题描述

我有以下8个下拉列表的列表



学生ID下拉列表1

课程下拉列表2

Rank Dropdownlist3

教师ID下拉列表4

学院名称Dropdownlist5

教师费率下拉列表6

Practicals Dropdownlist7

预订课程Dropdownlist8



i想验证是否未选择任何一个Dropdownlist。我想显示消息找不到一些细节。



我怎样才能在asp.net中使用c#进行验证。





问候,

Narasiman P.

解决方案

你可以使用 RequiredFieldValidator



参考 - / ^ ]。



否则,在某些按钮单击服务器事件中,检查<$ c的 SelectedValue $ c> DropDownLists 并相应地显示消息。


除了Tadit的解决方案,下拉列表的验证也可以在javascript中完成。

试试这个:

 <   asp:dropdownlist     id   =  DropDownList1    runat   =  服务器 >  
< / asp:dropdownlist >



Javascript:

< script language =   javascript type =   text / javascript> 

function Validate(){

if document .getElementById(' <%= DropDownList1。 ClientID%>')。value == 选择){
alert( 请选择DropDownList1!);
document .getElementById(' <% = DropDownList1.ClientID%>')。focus();
return false ;
}
}
< / script>



提交按钮:

 <   asp:按钮    id   =  Button1    runat   =  server    text   = 按钮    onclientclick   =  return Validate();  /  < span class =code-keyword>>  


位于<我风格=颜色:红色> Tadit Dash 解决方案。



你可以在 Javascript 还...

示例:



 <   html    < span class =code-attribute> xmlns   =  http://www.w3.org/ 1999 / xhtml >  
< head runat = 服务器 >
< script src = jquery-1.10.2.js type = text / javascript > < / script >
< script type = text / javascript >

var Check = function(){
var ddls = ['<% = DropDownList1。 ClientID %> ','<% = DropDownList2.ClientID %> '];
for(var i = 0; i < ddls.length; i ++) {

var ddl = document.getElementById(ddls [i]);

如果 (ddl.value = = '-----选择---') {

ddl.focus();

alert('some 详细信息 找到');

return false;

}



} < span class =code-attribute>

< span class =code-attribute> return true;

< span class =code-attribute> }





< / script >
< / head >
< < span class =code-leadattribute> body >
< 表格 id = form1 runat = server >
< div >
< asp:按钮 ID = Button1 runat = server OnClientClick = return Check(); OnClick = Button1_Click

< span class =code-attribute> 文字 = 添加到网格 / >
< br / >
< asp:DropDownList ID = DropDownList1 runat = 服务器 >
< asp:ListItem 文本 = -----选择--- / >
< asp:ListItem 文本 = Item1 / >
< asp: ListItem 文本 = Item1 / >
< / asp:DropDownList >
< < span class =code-leadattribute> asp:DropDownList ID = DropDownList2 runat = server >
< asp:ListItem 文字 = -----选择--- / >
< asp:ListItem 文本 = Item1 / >
< asp:ListItem 文本 = Item1 / >
< / asp:DropD ownList >
< br / >
< / div >
< / form > ;
< / body >
< / html >


i have list of 8 dropdownlist as follows

Student id Dropdownlist1
Course Dropdownlist2
Rank Dropdownlist3
Faculty id Dropdownlist4
Faculty name Dropdownlist5
Rate for faculty Dropdownlist6
Practicals Dropdownlist7
Booking for course Dropdownlist8

i want to validate if any of the one Dropdownlist is not selected. i want to show the message "some of the details are not found".

for that how can i validate in asp.net using c#.


Regards,
Narasiman P.

解决方案

You can use RequiredFieldValidator.

Refer - asp.net dropdownlist validation using Required Field Validation[^].

Otherwise, in some Button Click Server Event, check the SelectedValue of the DropDownLists and show message accordingly.


In addition to Tadit's Solution, Validation for dropdownlist can be done in javascript also.
Try this:

<asp:dropdownlist id="DropDownList1" runat="server">
                 </asp:dropdownlist> 


Javascript:

<script language="javascript" type="text/javascript">

         function  Validate() {

             if (document.getElementById('<%= DropDownList1.ClientID%>').value == "Select") {
                 alert("Please Select DropDownList1!");
                 document.getElementById('<%= DropDownList1.ClientID%>').focus();
                 return false;
             }  
         }     
    </script>


Submit Button:

<asp:button id="Button1" runat="server" text="Button" onclientclick="return Validate();"/>


on top of Tadit Dash Solution.

you can try this in Javascript also...
Sample:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        var Check = function () {
            var ddls = ['<%= DropDownList1.ClientID %>', '<%= DropDownList2.ClientID %>'];
            for (var i = 0; i < ddls.length; i++) {

                var ddl = document.getElementById(ddls[i]);

                if (ddl.value == '-----Select---') {

                    ddl.focus();

                    alert('some of the details are not found');

                    return false;

                }



            }

            return true;

        }





    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClientClick="return Check();" OnClick="Button1_Click"

            Text="Add to Grid" />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Text="-----Select---" />
            <asp:ListItem Text="Item1" />
            <asp:ListItem Text="Item1" />
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem Text="-----Select---" />
            <asp:ListItem Text="Item1" />
            <asp:ListItem Text="Item1" />
        </asp:DropDownList>
        <br />
    </div>
    </form>
</body>
</html>


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

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