I have a custom validator and some other validators on the page. But whenever I click the submit button for first time it only fires the custom validator and when I click the button for second time it's validating rest of the validators. Please let me know if you have any solution.
Thanks
Check your Page_Load to make sure you are not hiding or enabling something after the second call. I had a similar problem before and it confused the heck out of me until I realized I was manipulating a Panel in the Page_Load that contained the validator.
Other than that, you would need to post code (your Page_Load and Click event).
On Client Side when
OnClientClick="return SomeCustomClientCode();"
Is called, asp.net validators e.g required field validators are disabled and it does not gets listed in validators collection and does not validate the field validated by this validator and page post backs if custom validation passes.
To avoid this explicitly enable asp.net validators in Custom validation code or else where so that it gets activated before page postback or in the begiining of custom validation as follows:
ValidatorEnable(document.getElementById('<%=rfvDDLStatus.ClientID%>'), true);
rfvDDLStatus ==> required field validator which was not firing.. ValidatorEnable ==> Client API to enable asp.net validator
Related
I need to disable the submit button, to prevent double posting.
I added the following attributes to the asp:button:
UseSubmitBehaviour="false"
OnClientClick="this.disabled='true';this.value='Please wait ...'"
This works only if on the first click all controls of the form are valid. But if any one of the asp:RequiredFieldValidor fails, the button is disabled until the page is refreshed.
Use the below code:
OnClientClick="if (Page_ClientValidate()) { this.disabled='true';this.value='Please wait ...'}"
Explanation
Page_ClientValidate() This method is used for asp.net client side validation. So, if you page in client side validated then only disable your text box.
Okay, so I have my asp.net page with all of my comparison and requiredfield validators. This leaves me with two concerns.
What additional validation do I need? Do I need anything in the code behind? I want them to be unable to hit the 'save' button until their textbox information is complete, and it seems to be doing this with just the validator controls, but I'm unsure if there are other steps I need to take.
If I have a requiredfield validator and I want to turn it off under special circumstances, where in the codebehind would I set it to true? Can I do it on the 'save' button click, before it prevents the button from functioning?
1.
You need as many validations as necessary, you can create many different validators.
You need server-side validation in the code behind. If a postback occurred then the form passed the validation, but there are some validation features which are unusable at client-side. For instance you register to a homepage and you have a form where the username is required and there is a regular expression validator too. These validators will run at client-side. But if the username has to be unique and you can only check that using a database then obviously this can't be checked at the client-side, therefore, the client-side will evaluate the page to be valid, a postback will occur and it's the job of the server-side to check whether the username is unique.
Note that you can create custom validators if you need to do anything exotic.
2.
Depending on your needs you can set the Enabled property of your validators whenever you need to do that. Read more about that here.
As per my knowledge,
We should include a check for "Page.IsValid" on the server side (code behind) whenever we are using the ASP.NET Validators. This would ensure a check at the server side even if the javascript is being disabled on the browser.
No, you can't do that on the save button click as the button click would not be hit until the validation passes.
Hope this Helps!!
1 if you want add validation server
protected void Button1_Click(object sender, EventArgs e) {
//Proceed only if the validation is successfull
if (!Page.IsValid) {
return;}
}
2 You can set CausesValidation="false" to button
The #1 question with Page.IsValid is ok.
The requiredfieldvalidator is a javascript validator, so you can't disable it from codebehind once it was enabled (unless it satisfies the condition and you can go through). But it's possible to disable it via javascript, check this code:
ValidatorEnable(workPhoneValidator, false);
Link: Dynamically enable or disable RequiredFieldValidator based on value of DropDownList
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 am having a problem where my button click event is still firing even though my custom server-side validation is set to args.IsValid = false. I am debugging through the code and the validation is definitely being fired before the button click, and args.IsValid is definitely being set to false once the custom validation takes place, but it always makes its way to the button click event afterwards. Any ideas on why this is?
Not sure 100% of the particulars, but to prevent code from continuing, add to your button click event handler:
if (!Page.IsValid)
return;
That will prevent the code from executing.
On Client Side when OnClientClick="return SomeCustomClientCode();" is called, asp.net validators e.g required field validators are disabled and it does not gets listed in validators collection and does not validate the field validated by this validator and page post backs if custom validation passes...
To avoid this explicitly enable asp.net validators in Custom validation code or else where so that it gets activated b4 page postback or in the begiining of custom validation as follows:
ValidatorEnable(document.getElementById('<%=rfvDDLStatus.ClientID%>'), true);
rfvDDLStatus ==> required field validator which was not firing.
ValidatorEnable ==> Client API to enable asp.net validator
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?