Link to home
Start Free TrialLog in
Avatar of jknj72
jknj72

asked on

ASP.NET RequiredFieldValidator

I have a screen that a user can either enter information(about 5-6 fields) and click Add Item or they can enter in an ID(one of the fields) and click Search and it will populate all the other fields. This works great. I had RequiredFieldValidators for the fields that are mandatory(all of them) and I noticed that when I tried to do a search that the fields would have the validation messages next to them and it would never hit the code behind for the search button. Obviously the validator is working on the client side and doing its job but it wouldnt let the server side code to run? I want to have the functionality of the search button to fill the fields with data and for the RequiredFieldValidators to work when the Add Item is clicked.
 Now, I read where you can set EnableClientScript="true" in the validators(client side) and that allows the server side code to run when I click Add Item but then the validators are allowing missing fields. I tried to set the EnableClientScripts back to false but when I test and click Add Item, with no data, its saving a blank record and thats not cool!!!
Any thoughts on this? Any help would be appreciated!!!

Thanks
JK
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Use ValidationGroups.
Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.

You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.

So in your case:
(1) Create set the ValidationGroup for all the input controls and the Add button to the same value e.g. "ValidateForAddition"
(2) Since the search button is not in this validation group, it will NOT cause the validation to fire AND the code will be hit AND the input control values will be submitted with the form as well.

Here are some additional articles:
http://www.binaryintellect.net/articles/13427d3d-1f98-4dc0-849b-72e95b8b66a2.aspx
https://www.aspsnippets.com/Articles/Validate-Multiple-Validation-Groups-with-one-Button-in-ASPNet.aspx
Avatar of jknj72
jknj72

ASKER

Thanks for the response. I will test this first thing on Monday and let you know how it goes.
Thanks again
JK
TYPO: (1) Create set the ValidationGroup for all the RequiredFieldValidation (or any validators involved) (not input as I had said earlier) controls and the Add button's ValidationGroup  to the same value e.g. "ValidateForAddition"
Avatar of jknj72

ASKER

Ok thanks for the clarification
Avatar of jknj72

ASKER

MlandaT: I wanted to make sure Im setting the validation Groups correctly. Instead of setting ValidationGroups for the controls themselves, set them for the RequiredFieldValidators? So this would take care of my CVGID text box? And then I would do the same for all the other controls I need to have validated. Is what I have below correct?  ValidationGroup="ValidateControlsWI"

Thanks so much for your help

 <tr style="vertical-align:central;">
                    <td align="center" style="vertical-align:central;">
                        <asp:Label ID="CVGIDLabel" runat="server" AssociatedControlID="CVGID">Coverage ID:</asp:Label>
                    </td>
                    <td class="auto-style6" style="vertical-align:central;">
                        <asp:TextBox ID="CVGID" runat="server"></asp:TextBox>
                        &nbsp;
                        <asp:Button ID="btnCVGID" runat="server" Text="Search" BackColor="#46A3FF" ForeColor="White" Font-Bold="True" OnClick="btnCVGID_Click" Height="30px" />

                        <br />
                        <asp:RequiredFieldValidator ID="CVGIDRequired" runat="server" ControlToValidate="CVGID" SetFocusOnError="true" Enabled="false" EnableClientScript="true" ErrorMessage="* Coverage ID is required." ForeColor="Red" ToolTip="Coverage ID is required." ValidationGroup="ValidateControlsWI"></asp:RequiredFieldValidator>  
                        
                    </td>
                </tr>

Open in new window

Avatar of jknj72

ASKER

FYI, I took out the EnableClientScript=True for now so I can narrow my problem down. So if you see it anywhere please disregard
Avatar of jknj72

ASKER

I also took out Enabled=False so you can disregard this too. Sorry its early and its a Monday !!!
Avatar of jknj72

ASKER

Ok I am testing this and it does work but my only issue, for right now, is that the server code seems to run before the client code does its thing. For instance, if I come to the screen for adding an item and I simply click on "Add Item" the btnAddItem_Click event fires prior to the Vaildation code on the client side runs to show the messages that say that the fields are required. I am going to try and do some validation in the code behind first and then see if I can somehow get my controls to fire in the order I need.
In a perfect scenario I want the following to happen in this order. On click of the Add Item button, I want the FieldValidators to run first and not allow the server side code to run until the validation returns true. Then when all required fields are met to run the Add Item code and save the data entered. . Hope Im not babbling and you understand what Im trying to do.
Don't forget to put back the EnableClientScript=True

How does the ADD button work? Is it doing a Javascript based method to submit  the form?
Avatar of jknj72

ASKER

Add the EnableClientScript = True where? And Im not using javascript method to submit anything. I saw code that fires a javascript event to check the ValidationGroup controls for values. I tried that and it worked but the ValidationGroup would depend on a Session variable and I couldnt figure out how to use it on the Client Side? Im gonna continue to use your way with the ValidationGroups and see what happens. Ill keep you updated. Thanks

JK
Avatar of jknj72

ASKER

Ok I switched things up a little bit. I have a javascript function that seems to work for me. The only thing is I would like to pass in an id or variable so the program knows what to check. I probably shoulda mentioned this already but I didnt think I was gonna have to. I have a popup.aspx form that has 3 HTML tables(workitems, travelitems and imageitems) and depending on what they are trying to add is dependent upon what will show(3 radio buttons tells me what to display). If they select work items then the table id="workitems" will be set to visible and the others are set visible=false(I set Session("ItemTypeID") = 1 for work items and 2=Travel and 3=Image).
All this works fine. When I click on Add Item I call the javascript function OnClientClick=Validate() that will check my ValidationGroups and do its thing.

    <script type="text/javascript">
        function Validate() {
            var isValid = false;
            isValid = Page_ClientValidate('ValidateControlsWI');  \\work items
            if (isValid) {
                isValid = Page_ClientValidate('ValidateControlsTI');  \\travel items
            }
            if (isValid) {
                isValid = Page_ClientValidate('ValidateControlsII');  \\image items
            }
            return isValid;
        }
    </script>

Open in new window


My problem is how can I check either a Session variable, or whatever else, to see which table I want to do Validation for? I cant check all 3 everytime obviously so I need to just check the fields that are in the visible table. And yes, I was thinking maybe I can check to see which table is currently visible but didnt think that would be the best way to go????
How does the popup know what they are trying to add? You must be passing in a value that allows the popup.aspx to know what to do.

Store that value in a hidden field and check the value of that hidden field in the Javascript to determine what logic to execute.
Remove the CauseValidation property (CauseValidation=False) from the search button and let the Field Validators work on add item click. On this way the search button won't trigger a validation and the add button will trigger the validation and do what you want before add it to the database.
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's right. try to do our suggestion and let us know how did it work for you. Always we will be here for help
Avatar of jknj72

ASKER

I stayed with the JavaScript function but changed the Validation group of the Add button like you suggested. Works like a charm.
 Thank you both for your help

JK
Avatar of jknj72

ASKER

Validation Group was the key to the answer. Thanks again