Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario:
Gridview has items which should only be enabled if first field's dropdown is set to a specific value (call this field1). To start, textBoxField1 and dropdownList1 are disabled. If field1 value is set, textBoxField1 and dropdownList1 are enabled in the code behind. If textBoxField1 is not filled in and nothing is selected in dropdownList1 when Add linkButton is clicked, the asp:RequiredFieldValidator for each of these does not show an error and the asp:CustomValidator does not fire. (It seems reasonable the asp:CustomValidator does not fire.)

The Add linkButton:
HTML
<FooterTemplate>
  <asp:LinkButton ID="AddLink" ValidationGroup="AddValidationGroup" runat="server"  CausesValidation="True" CommandName="Add" ToolTip="Add" >
<asp:Image ID="AddImage" runat="server" ImageUrl="~/Images/Add.png" />
  </asp:LinkButton>
</FooterTemplate>

Question:
How do I cause the asp:RequiredFieldValidator to fire?

Thank you in advance for your help.
Posted
Updated 21-Aug-12 7:00am
v4
Comments
[no name] 21-Aug-12 14:49pm    
your question is not clear..could you elaborate it ?

XML
in LinkButton set one property OnClientClick="return validateFields();"
add following function in  <script type="text/javascript"> block of your .aspx page
Here i assume that drop down name is Dropdown1 & RequiredFieldValidators Name is RequiredFieldValidator1 & RequiredFieldValidator1..
Make in appropropriate changes as per your naming..

 function validateFields(){
    var varBool = true;
    var isempty=$get('<%= Gridview1.FindControl("DropDown1").ClientID %>').value
    if(isempty != ""){
        if ($get('<%= Gridview1.FindControl("RequiredFieldValidator1").ClientID %>') != null) {
                var validator = $get('<%= Gridview1.FindControl("RequiredFieldValidator1").ClientID %>');
                ValidatorEnable(validator, true);
        ValidatorValidate(validator, false);
              varBool = varBool && validator.isvalid;
              return varBool;
            }
        if ($get('<%= Gridview1.FindControl("RequiredFieldValidator2").ClientID %>') != null) {
                var validator = $get('<%= Gridview1.FindControl("RequiredFieldValidator2").ClientID %>');
                ValidatorEnable(validator, true);
        ValidatorValidate(validator, false);
              varBool = varBool && validator.isvalid;
              return varBool;

            }
    }
    return varBool;
}
 
Share this answer
 
v2
When you dont want to do validation just disable validator.
Use this while disabling control
C#
RequiredFieldValidator1.Enabled = false;


When you want to do validation just enable validator.
Use this while enabling dropdown & textbox control.
C#
RequiredFieldValidator1.Enabled = true;
RequiredFieldValidator1.Validate();


if you want to check validation status manually

C#
RequiredFieldValidator1.IsValid
 
Share this answer
 

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