更新时间:2023-11-23 17:45:22
你可以使用RequiredFieldValidator
。
参考 - / =http:// www。 c-sharpcorner.com/Blogs/5385/Asp-Net-dropdownlist-validation-using-required-field-validat.aspx\"target =_ blanktitle =New Window> ^ ]。
否则,在某些按钮单击服务器事件中,检查 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(); / >
位于 Tadit Dash 解决方案。
你可以在Javascript
还...
示例:< html 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 = = '-----选择---') {n>
ddl.focus();
alert('some 详细信息 不 找到');
return false;
}
}
return true;
}
< / script >
< / head >
< body >
< 表格 id = form1 runat = server >
< div >
< asp:按钮 ID = Button1 runat = server OnClientClick = return Check(); OnClick = Button1_Click
文字 = 添加到网格 / >
< br / >
< asp:DropDownList ID = DropDownList1 runat = 服务器 >
< asp:ListItem 文本 = -----选择--- / >
< asp:ListItem 文本 = Item1 / >
< asp: ListItem 文本 = Item1 / >
< / asp:DropDownList >
< asp:DropDownList ID = DropDownList2 runat = server >
< asp:ListItem 文字 =pan> -----选择--- / >
< 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 useRequiredFieldValidator
.
Refer - asp.net dropdownlist validation using Required Field Validation[^].
Otherwise, in some Button Click Server Event, check theSelectedValue
of theDropDownLists
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 inJavascript
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>