Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
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:
<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 21-Aug-12 5:28am
Edited 21-Aug-12 8:00am
v4
Comments
learner'sbug at 21-Aug-12 14:49pm
   
your question is not clear..could you elaborate it ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

When you dont want to do validation just disable validator.
Use this while disabling control
RequiredFieldValidator1.Enabled = false;
 
When you want to do validation just enable validator.
Use this while enabling dropdown & textbox control.
RequiredFieldValidator1.Enabled = true;
RequiredFieldValidator1.Validate();
 
if you want to check validation status manually
 
 RequiredFieldValidator1.IsValid
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 23 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100