Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,


Can any one help me how to enable or disable the required field validator control based on selection of Dropdown list in asp.net 3.5.I have worked with the same peice of code in asp.net 2.0 and there it is working fine.



Regards,
P.Sri Pavan
Posted
Comments
jim lahey 10-Nov-10 6:06am    
if it works in 2.0 you should be ok in 3.5 also
Ankur\m/ 10-Nov-10 6:32am    
As Jim said, it should work fine with 3.5 also.
Are you getting any error? Can you put up your relevant code here?

Hi,

Enable or Disable ASP.Net Validation on client side
rfvOther: is a Required Field Validator
Collapse

C#
//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);
//Explanation:ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)
//Example:
ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false);


You can implement this function according you logic.


Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
You can easily disable the validator on the server side

Follow the example:

XML
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true">
            <asp:ListItem Value="1">One</asp:ListItem>
            <asp:ListItem Value="2">Two</asp:ListItem>
            <asp:ListItem Value="3">Three</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txt" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="Validator1" runat="server" ControlToValidate="txt"
            ErrorMessage="error"></asp:RequiredFieldValidator>
        <asp:Button runat="server" Text="Save" />



and this piece place into your server side page class:

C#
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Validator1.Enabled = Request[DDL1.UniqueID] == "2" ? false : true;
    }
}


When you select "Two" in your dropdown, it will disable the validator
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900