UPDATE : Figured it out..
The objects I was passing to the ValidatorHookupControl were'nt being set properly (were null). Now that they are, the messages are currectly dissapearing when the hooked up control looses it's focus.
ORIGINAL POST ..
Hi,
I have some ClientValidation controls that have ClientSideValidation methods which work fine when validating the page..
However, how can I make it so that when a certain control that a CustomValidators clientside method kicks in and udates the validation message depending on whether the validation has passed or not. (Like the RequiredFieldValidator or RegExValidator).
My Customvalidators do not have their ControltoValidate properties set as some of them depend on multiple controls.
I don't want any postbacks (full or partial).
I have tried..
Adding an onchange attribute on dropdowns, radioboxes and checkboxes that call a helper clientside method which calls Page_ClientValidate('GroupName'), then setting window.location back to the control in question (as it went back to top of screen).
Using this method the args.IsValid is still being set by the ClientSideValidation method.
And I have tried ValidatorHookupControl (control, validator) but that doesn't seem to work either.
Any thoughts..?
My bad, was passing null objects into the ValidatorHookupControl method.
Works now. Doh!
Related
I want to know how ASP.NET validation controls are working (eg: requiredfield validator, regular expression validator). What will really happening when clicking on the button? What are the events are fireing on client side? What really happening internally?
When you click on the button (if you you defined CausesValidation="true") button send request to server with __doPostBackWithOptions function aims which contains valiadtion group as parameter (if you defined ValidationGroup property for your button). __doPostBackWithOptions contains function Page_ClientValidate. In this case will be checked all validators which belong for this group. If result (Page_IsValid) will be true postback occures. If You don't define validationGroup (but CausesValidation true) will be checked all validators which is present on page. If defined CAusesValidation false will use _doPostBack function without validators checking.
More information here: http://msdn.microsoft.com/en-us/library/aa338815(v=vs.71).aspx and
http://msdn.microsoft.com/en-us/library/aa479045.aspx
I have a form where I dynamically populate a DropDownList using Jquery's ajax function to retrieve a list of values from a web service. I originally had a Button control which submitted the form. This caused the "exception:Invalid postback or callback argument. Event validation is enable...." error.
After researching options, such as disabling event validation (bad) and registering for event validation (which would not work in this case) the best option seemed to be to swap the Button control for a LinkButton control. I did this and, sure enough, it works fine now.
My question is...why?
What is different about the LinkButton that means that it does not cause the event validation error and have I, by changing to a LinkButton, introduced a new security risk because event validation isn't happening?
The postback validation error is happening because the data you send back at the postback is no the same than when it was sent by the server.
You should take a look at this blog post by Scott K. Allen. He suggests to add all the possible values for your dropdown in the Render event for your web page.
You could also create your own version of the DropDownList since it won't require event validation as this guy suggests.
My personnal take is that you might have to rethink how you interact with your data. If you need to feed dynamically your DropDownList and you use ASP.NET WebForms then you are required to have a PostBack for that. You could use a UpdatePanel to make it feel "Ajax" if you want.
I have a RegularExpressionValidator for a TextBox in a control, which itself is part of another control. When I click the button to submit the form, it seems that it should not do so unless all child controls are properly validated. However, what ends up happening is that I see the validation error message pop up for each control that failed to validate before the page posts back anyway and fails when it can't parse the malformed input.
I have tried surrounding the failing code with if (Page.IsValid) {...} to make sure it doesn't run without complete validation, but the property ends up being true by the time I hit the breakpoint.
Shouldn't an entire page be invalid if any child controls are not successfully validated?
Do you have different ValidationGroup controls defined? As long as the validators in the same validation group as the button are all setup correctly, yes it should block. Unless, for some reason, the JS is failing to load for the validators.
HTH.
Set "CausesValidation = true " to your submit button, I guess your problem will be solved.
Have you called Page.Validate() before using Page.IsValid ?
I have created a custom control and a custom validator (extending BaseValidator). On custom control I have set ValidationProperty("Values"). The problem is that validation doesn't work when postback is sent unless I execute Page.Validate(). And when I call Page.Validate() all validators are executed which is not what I would expect on postback.
How do I create custom validator which would be executed when control value changes and validates just that control?
That is not how validators work. Unless you are using a ValidationGroup setting, all the validators on your page will automatically fire. You do NOT have to explicitly call Page.Validate(). You DO need to wrap your code in a check like this, however:
if(Page.IsValid)
{
//do something here
}
Unlike client-side validators, the server-side validation does NOT prevent the page from posting back and processing events as normal.
To create a control which only validates when the control value changes would require a bit of hackery, since the change event fires after the validators have been executed.
Have you tried using validation groups?
I've used the following in web.config
<pages enableEventValidation="false">
This corrects a problem we've been having with Ajax.
We have a web page that if you browse to directly using a standard HTML hyperlink works fine.
If you browse to the page from another page via link inside a gridview and response.redirecting in the RowCommand event to the page passing an ID in the querystring. The page throws errors from controls inside the panel stating
"Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. "
I'm happy to leave the page validation as false as it seems to have had no other effect.
Any ideas what's happening?
Read the documentation.
EDIT: For security reasons, it's probably best to leave it set to true wherever you can.
I would therefore recommend that you set it to false only on the individual AJAX pages where it causes problems, while leaving it true in web.config.
From here
Invalid PostBack or CallBack argument error is basically raise because of Event Validation feature. The EventValidation feature is a new feature in ASP.NET 2.0, and provides an additional level of checks to verify that a postback from a control on the client is really from that control and not from someone malicious using something like a cross-site script injection to try and manipulate things. It is part of our overall strategy of increasingly adding security in depth levels to the programming model -- so that developers can be secure by default even if they forget to add security checks of their own.
Now, Invalid PostBack or CallBack argument error may occur when you are firing click event and the object is rebinding or its properties are changed in Page_Load event or someone is trying to hack into your system with cross site scripting. Each time .Net Framework render a page then it associate a unique Guid for all the controls. When binding a gridview or repeater, on each databind framework will associate a new guid for the contorl. So every time when you are firing event make sure Page_Load event does not change the control, because if the control changed the it will have a different Guid which have acutally fired the event for postback. Here are some scenario with this error.
1) Invalid Postback or Callback argument in GridView Problem may be: You are binding data in Page_Load event with either Object Data Source or Manual Binding with function call. This will make your GridView bind data on every event fire of any control. When you are firing any GridView command with OnRowCommand, before RowCommand fire your GridView will rebind and all control within it will be assigned to new id. So RowCommand could not get the item which have fired the event. Solution for Invalid Postback or Callback argument in GridView: You can bind your data within this if condition
if (!IsPostBack)
{
//Your code for Bind data
}
This code will definitely give you solution if this not work then check whether any other control is not giving error.
There is one thing worth adding here: If you want to disable the event validation for a specific control, rather than the entire page, there is a workaround documented here and here (and now referenced in the relevant Connect suggestion):
Simply subclass the relevant WebControl class, and don't set the SupportsEventValidation attribute on the subclass. The subclass will be exempt from event validation.