如何在下拉列表中验证默认值 [英] How to validate default value in dropdown list
本文介绍了如何在下拉列表中验证默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目中有一个下拉列表。这包含从数据库中选择的许多项目。下拉列表中的第一项是选择,其余项目来自数据库。我想在选择第一个项目时应用验证。我正在使用ASP.NET(C#)。
我该怎么办?请帮帮我...
I have a drop down list in my project. That contains a lot of items selected from database. The first item in drop down list is "select" and remaining items come from database. I want to apply validation when the first item is selected. I''m using ASP.NET(C#).
How can i do this??? Please help me...
推荐答案
var ObjDrp1 = document.forms[0]['ctl00_ContentPlaceHolder1_ddlGroupname'];
var Idx = parseInt(ObjDrp1.selectedIndex);
if (Idx == 0)
{
alert ('Please Select Type ...!!!');
ObjDrp1.style.backgroundColor='LemonChiffon';
ObjDrp1.focus();
return false;
}
这是通过javascript验证的方式
This is the way To validate by javascript
使用必填字段验证器&将其Initialvalue属性设置为下拉列表的第0个位置值。 (这里 - 选择 - 是那个值)
Use required field validator & set its Initialvalue property to 0 th postion value of dropdown. (here --Select-- is that value)
<asp:dropdownlist id="ddlstatus" runat="server" >
<asp:listitem>--Select--</asp:listitem>
</asp:dropdownlist>
<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" errormessage="Please select status" controltovalidate="ddlstatus" validationgroup="btnAdd" initialvalue="--Select--" ></asp:requiredfieldvalidator>
使用Javascript:客户端验证
====== ===================================
< script type =text / javascript>
函数validate()
{
var ddllist = document.getElementById(< ;%= DropDownList1.ClientID%>)。value;
if(ddllist.options [ddllist.selectedIndex] .value ==''选择''){
提醒(''选择一个值'');
返回false;
}
}
< / script>
称之为va lidate function onclick of dropdown或onclientclick of button
------------------------- -------------------------------------------------- ------
服务器端验证:
=============== =============
Hi,
using Javascript : Clientside validation
=========================================
<script type="text/javascript">
function validate()
{
var ddllist= document.getElementById(<%=DropDownList1.ClientID %>).value;
if (ddllist.options[ddllist.selectedIndex].value== ''Select'') {
alert(''Select a value'');
return false;
}
}
</script>
call this validate function onclick of dropdown or onclientclick of button
---------------------------------------------------------------------------------
On Server side validation :
============================
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "Select")
{
Lable.Text="Select any value";
}
}
这篇关于如何在下拉列表中验证默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文