How to retain ASP.Net validation summary on Postback - asp.net

I have a aspx page with multiple groups to be validated. validation on groups happen based on some conditions I have written.
Currently all my validation work fine with client side validation.
But, when I do a postback the validation summary is getting disappeared. In the post back I am enabling some additional controls which are not yet touched by the user( so validation should not happen on this newly visible controls).
so My problems are:
To Retain the Validation summary generated by client side script on post back.
Only Validation messages that are generated on client side should be visible.
Newly added field should not be validated on postback(fields that are untouched).
Please suggest....

In the post back event handler add the invalid validators by calling Page.Validate("Group");

To Retain the Validation summary generated by client side script on post back.
I think you should check ViewState.EnablViewState=true
Newly added field should not be validated on postback(fields that are untouched).
Use ValidationGroup.Apply different ValidationGroup for different input and related button.
Only Validation messages that are generated on client side should be visible.
Not fully understood.May be by solving 1 & 3 this will disappear .

Related

Required field validator of the textbox

I have one drop down and two text boxes for the start date and for the end date .The required field validator for the end date i'm enabling depending on the value of the drop down selected.
All of this validation part i'm doing inside the client side using the ValidatorEnable() built in function of the javascript which is calling due to onchange() function of the drop down.
All of this is working fine on the change of the value of the drop down first time. But when i'm doing the post back and during this there is a failure of some client side validation,the required field validator of the end date text box is not retained.
Lack of code makes it hard to diagnose, but it sounds like you are only enabling validation based on value of the dropdown selected and only when the user changes the value of the dropdown... just do that same validation call when the page loads and not just when they select an item from the dropdown list. So on page load if the dropdown list has a previously selected value, your validation should get enabled and work as expected.
Also you should fix it where there is no postback on client side validation failure.
Just use the Foolproof validation library that is available on Codeplex: https://foolproof.codeplex.com/
It supports, amongst others, the following "requiredif" validation attributes / decorations:
[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]
Specifically I would suggest that you use the [RequiredIfRegExMatch] attribute.
To get started is easy:
Download the package from the provided link
Add a reference to the included .dll file
Import the included javascript files
Ensure that your views references the included javascript files from within its HTML for unobtrusive javascript and jquery validation.

How to trigger ASP.NET client-side validations without submit?

I have a website in ASP.NET (WebForms, NOT MVC) which has a survey form divided in several slides. Each slide has a next button that, obviously does a transition (client-side, not post back or remote request) to the next slide.
In each slide I have several ASP.NET controls with their related validators. I want this validators to be triggered when I click the next button (or maybe when each input loses focus?).
I remembered ASP.NET doing client side validation on lost focus, but maybe I'm wrong... (I quit doing ASP.NET development about 3 years now, so I can't remember)
Thanks
UPDATE:
It would be better to make ASP.NET trigger each validator when the associated control lost focus. I remember ASP.NET doing this (or am I dreaming? =P)
First you need to make sure all of your validators have target controls specified using the "TargetControlID" Attribute on the validators.
Then you can set up a validation group per page and specify the group name in your next button and on the controls themselves.
If you are using regular expression validators you can choose them from this website
To Validate Client Side
If you are using custom validators you can create a client function and specify it on the custom validator using the ClientValidationFunction attribute and by setting EnableclientScript = "true" on the custom validator.
Just be sure your client function has sender and args parameters.
It looks like there's a supplied JavaScript function called Page_ClientValidate which should be callable to check the validation manually from JavaScript. I haven't used it, though, so YMMV.
put all your client-side validators into the same validationgroup and with your 'next' button add the same validation group. When you click the button it will automatically trigger all the validators before it does the post-back.
as to manually triggering the validation...
you might also be able to use ValidatorOnSubmit(). I remember doing this in another project but i'm having a hard time finding the code.
It seems that enabling 'SetFocusOnError' on each validator triggers the validation whenever I try to leave the field.
In short decorate your model, now Data Annotations are supported from Asp.Net 4.5
Check my Answer here..Client side webform validations

ASP.NET Validate all validators with different validation groups

I'm writing an ASP.NET page and trying to get validation working. My problem is that I've got a repeater that contains several custom grid controls, each of which has validators and a validation summary.
At first, I didn't assign any validation groups, but this ended up making validation summaries appear for every grid whenever there was an error in one (because the validation summary's validation group was not set, I suppose, which meant it captured all validation errors).
So I assigned a separate validation group for each grid, unique with respect to a certain property on the control. But I have a button at the bottom of the page (with no validation group) that needs to validate the input. Not having a validation group, it won't automatically validate the grids' validators, so I added a click handler that calls Page_ClientValidate(). No dice--validation appears everywhere.
Okay, so I iterate through the validation groups and call Page_ClientValidate(validationGroup) on each if it has any validators. Works fine when only one grid has validators, but when two or more do, it automatically hides all validation summaries but the last one checked. Is there any way to disable this behavior, or a better way to do this entirely?
If I need to, I guess I can go through and unhide the other validation summaries once I've finished iteratively validating (although that might possibly have other implications), but I'd also need to unhide the validator displays (I'm using an image to denote invalid fields). It seems like such an annoying and possibly fragile solution.
EDIT: Oh, a twist. I tried doing the approach I mentioned at the end--re-showing the hidden validators/validation summaries that are invalid--but the Microsoft code prevents that, too; in the first line of the ValidatorValidate(validator, validationGroup, event) method (called by Page_ClientValidate(validationGroup) on each validator in the page), validator.isvalid is set to true, and is only set to the return value of the validation function within a conditional that only runs if the validationGroup parameter matches the validator's. The upshot being, the hidden validators all are marked as valid, making it tough to determine after the fact whether a validator is valid because it's actually valid or because Microsoft was stupid in designing the client-side validation code.

How does RequiredFieldValidator automatically stops database being updated?

My previous impression of RequiredFieldValidator and similar have been that they show you an error label and set Page.IsValid false and that is it. Meaning they leave the rest of the task (preventing the use of wrong input data) to you. But today I have realised say if you use a DetailsView to insert a new record to a database, and you use validators to check the TextBoxes inside the DetailsView, they automatically prevent the database from being updated.
I would like to know how this is implemented behind the scene. I'm guessing it aborted the Page Lifecycle at Validator.PreRender event, so that database connections at later stage could not be reached? I'm probably wrong.
I'm trying to use Reflector to get inside the RequiredFieldValidator to see how it is implemented, but I don't really know where to look. Can someone give some hints?
This article explains the validation in detail for ASP.NET.
http://msdn.microsoft.com/en-us/library/aa479045.aspx
Validation can be both Server side and/or client side. If used, client side validation doesn't let user submit the form until it is validated. Client side validation is implemented using JavaScript and DOM. Every submit button is wired by the framework to check validation before doing post back.
Server side validation works differently. The event chain is extended between page load and event procedure call. The validation results are set, which can be interpreted by any event procedure.
In your situation I guess you have client validation on - which it prevents Form from being submitted to server and that's why no actual update to database is done.
EDIT: DetailsView control does support server side validation controls. Follow this link for details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#security

Using JSON or postback method?

I need to develop a page which has 2 dropdownlist.
Options of dropdownlist 2 are based on selection of dropdownlist 1.
I have 2 methods to change the dropdownlist 2. What will you choose?
1:
Postback when users select dropdownlist 1 and change dropdownlist 2.
Pros:
Can use the postback feature, can use the asp.net validator
Cons:
Need to communicate with server (more traffic)
Users will see the page loading in the status bar.
2:
Get all the data (not very much data) in a JSON object when loading the page and change the dropdownlist 2 using javascript.
Pros:
Don't need to communicate with server(less traffic)
Cons:
Can't use the postback feature and validator and more troublesome to write server validation.
Also, I usually write the JSON object to the page as follows:
var locations = <asp:Literal runat="server" id="litLocation" text="[]" />
And then set the "litLocation" in page_load after the data is processed by datacontractjsonserializer.
Do you do it in the same way?
I prefer the second option, no need to reload the whole page just to refresh one dropdown list. I'd also do the client side dev in jQuery, much easier. You can do the client side validation for the change event of the first dropdown in jQuery as well, and keep the form submit validation in ASP.NET.
Have a look at the selectChain plugin for jQuery (demo's etc here).
Why not have your javascript call the server when the select box is clicked on, using a GET method, and fill in the select box, using json as the response, then, when an option is picked then fill in the second select box with another ajax request.
This would be scalable, in that if you want to add more options you just change the server, and everything is centralized.
You will need to validate when the form is submitted anyway, as it is possible to change a value of a form to something illegal using some debugging tools, such as Firebug, so never trust anything from a webpage until you have validated it.
So, no point worrying about the validation until the form is actually submitted.

Resources