Statistics
Visits: 105082
Page Visits: 139157
Active visitors (monthly): 5364
Feed Count: 45
Most visited page: Adding value as a technical analyst
Archive : Aug 2010

Disable validation based on option

In a webpage, there are multiple validators and a dropdown. In the dropdown, if the option selected is Y, all validators should fire. If option selected is N, then all validators should be disabled. The server-side code is simple. How can this be done in javascript?

The HTML of the page looks like this:


 <asp:DropDownList ID="ddlList" runat="server" OnChange="ValidateIf();">
    <asp:ListItem Value="Y" Text="Yes"></asp:ListItem>
    <asp:ListItem Value="N" Text="No"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txtName"
                             ValidationGroup="Group1" Display="Dynamic"  
                             ErrorMessage="Please enter name" 
                             >
</asp:RequiredFieldValidator>
<asp:Button ID="btn" runat="server" Text="Submit" ValidationGroup="Group1" CausesValidation="true" />

When the dropdown is changed, then the javascript function - ValidateIf() is fired. This contains logic to enable / disable the validators. The function ValidatorEnable() is used for each validator


 function ValidateIf() {
    var list = document.getElementById('ddlList');
    var val = document.getElementById('val');
    
    if (list.options[list.selectedIndex].value == "Y") {
        ValidatorEnable(val, true);        
    }
    else {
        ValidatorEnable(val, false);
    }
}

Permalink | 2 Comments | Leave your comment
 
Commented by ray at 01-Oct-2010 07:33 AM
Javascript post #1
Commented by Shailendra at 29-Oct-2010 01:28 PM
What is special about this post?

A Simple XmlProfileProvider

A simple XmlProfileProvider can be created without any membership provider. This MSDN article provides the basics of creating a Custom Profile Provider. The most important methods to override are: Initialize, GetPropertyValues, SetPropertyValues, GetProfilesByUserName, GetProfiles. The rest of the method can be implemented for the sake of completeness (of implementing the ProfileProvider base class). The following zip file contains a sample implementation of the simplest XmlProfileProvider. This provider supports only the basic primitive types, String, DateTime. Complex objects are serialized using .ToString()

Sample XmlProfileProvider
Permalink | No Comments | Leave your comment
 

Prevent multiple AJAX postbacks

To prevent multiple AJAX postbacks at the same time, the following code can be used:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_BeginRequest(abortPostbacks);

function abortPostbacks(sender, args) {
    if (prm.isInAsyncPostBack)
        args.set_cancel(true);
}
Sys.WebForms.PageRequestManager is the core class of Microsoft AJAX library. It takes care of AJAX life cycle at the browser. It has lot of useful methods and events. In the above code, we can trap the BeginRequest event that is fired for each AJAX request. If there is an already an AJAX request (isInAsyncCallBack), the current request can be cancelled.
Permalink | 3 Comments | Leave your comment
 
Commented by Måns at 14-Feb-2011 05:50 PM

Correct syntax is:


var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(abortPostbacks);

function abortPostbacks(sender, args) {
   if (prm.get_isInAsyncPostBack())
       args.set_cancel(true);
}

Commented by Vijay at 05-Feb-2011 07:00 PM

The function should be called for all ajax calls within the page which should not be invoked when another ajax call is running in the page.

Commented by muzammil ahmed at 28-Jan-2011 11:23 AM

Where do you need to add this piece of code? thanks.