使用getElementById获取DropDownList的值__使用ValidatorEnable控制Validator控件

 function   getData()  
  {  
                var   province=document.getElementById("DropDownList1");  
                var   pindex   =   province.selectedIndex;  
                var   pValue   =   province.options[pindex].value;  
                var   pText     =   province.options[pindex].text;  
  }  

 

 

==============================================

 

 

Sometimes we need to Enable or Disable validation on client side.For that use ValidatorEnable function in the Asp.net javacsript Script Library.

For that set EnableClientScript property of validator to True.

Here i give example for this:

I have a page with a couple of radio buttons.On radio button selection i want to enable/disable validation.

In example if i select Email radio button then Email div will display and only txtEmail textbox validator is enabled.

Java Script for this:

<script language="JavaScript" type="text/javascript">
function autoSelect(control,type)
{
if(type=="Email")
{
document.getElementById('Email').style.display="block";
document.getElementById('PhoneNo').style.display="none";
ValidatorEnable(document.getElementById("RequiredFieldValidator1"), true);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), false);

}
else
{
document.getElementById('Email').style.display="none";
document.getElementById('PhoneNo').style.display="block";
ValidatorEnable(document.getElementById("RequiredFieldValidator1"), false);
ValidatorEnable(document.getElementById("RequiredFieldValidator2"), true);
}
}
</script>

Code for this:


Email :
<input type="radio" id="RadioButton1" runat="server" value="Plan1" name="Plan" οnclick="autoSelect(this,'Email')" checked />
PhoneNo :
<input type="radio" id="RadioButton2" runat="server" value="Plan1" name="Plan" οnclick="autoSelect(this,'PhoneNo')" />
<div id="Email">
Email <asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Email Required"
ControlToValidate="txtEmail" EnableClientScript="true" ValidationGroup="vgSubmit" />
</div>
<div id="PhoneNo" style="display:none">
PhoneNo <asp:TextBox ID="txtPhoneNo" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="PhoneNo Required"
ControlToValidate="txtPhoneNo" EnableClientScript="true" ValidationGroup="vgSubmit"/>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="vgSubmit"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值