Page.Validate doesn't display ValidationSummary when IsValid = false - c#

I know there are many questions similar to this but I've been unable to find one that fits my situation.
I have a basic form with a few TextBoxes and RequiredFieldValidators and an ASP Button to submit the form. I have set the submit button CausesValidation="false" because I want to perform some functionality in the event that the form is not valid. All validators have an ErrorMessage set and all validators, and the summary have the ValidationGroup="Registration" set. The ValidationGroup is on the submit button as well but I don't know if that makes any difference when CausesValidation is false.
In the button click event I am calling
Page.Validate("Registration");
if (!IsValid)
{
}
I have set breakpoints and IsValid is definitely false and it gets into that code block, but the ValidationSummary doesn't show the error messages from the Validators. I can't figure out why.
The ValidationSummary is defined as follows
<asp:ValidationSummary CssClass="validatorSummary" DisplayMode="BulletList" ID="valSummary"
runat="server" EnableViewState="false" EnableClientScript="false" ValidationGroup="Registration" HeaderText="test" />
Is there anything that could cause this kind of behaviour? I feel it must be something very simple I'm missing here. Any help would be much appreciated.

Related

onclick event is not fired by input buttons inside a user controls

I've updated my .Net web application to use Framework 4.5, after the update, all the input buttons (not asp:Buttons), have stopped firing the onclick javascript code, this is only happening on those buttons that are inside a user control (.ascx).
Just for the record, user controls are neither being loaded dinamically nor inside update panels.
My buttons look like this
<input id="cb" onClick="myfunc()" type="button" value="Close" />
My user controls are included to the page as follows
<cc:actionbar id="theActionBar" runat="server"></cc:actionbar>
and the javascript function, which is also included within the user control, is
function myfunc() {
if (confirm("Before closing, please make sure you saved any changes.\nAre you sure you want to close?") == true) {
__doPostBack('theActionBar:theClose', '');
}
}
this works just fine on Framework 3.5 and previous versions.
any idea why is this happening??? or how can I solve this?? I have tried several suggestions I've found over the internet and nothing seems to work.
Thanks in advance.
.
I can't see an obvious reason, but have you considered simplifying your approach to avoid the custom javascript and hard-coded postback event reference? You can get exactly the same behaviour with an ASP.NET button's OnClientClick property:
<asp:Button runat="server" ID="btnClose" Text="Close" OnClick="btnClose_Click" OnClientClick="return confirm('Before closing, please make sure you saved any changes.\nAre you sure you want to close?')" />
Returning false from the OnClientClick code or function prevents the postback.
Switching to this approach may be preferable and may even solve your issue if it's something to do with the postback event reference.

Validation prevent check box to auto postback

I have following checkboxes on my page:
<asp:CheckBox ID="rbBuilding" runat="server" AutoPostBack="True" OnCheckedChanged="HandlerPackageOnCheckedChanged" Checked="True" CausesValidation="False" />
<asp:CheckBox ID="rbContent" runat="server" AutoPostBack="True" OnCheckedChanged="HandlerPackageOnCheckedChanged" CausesValidation="False" />
There is an logic on postback based on combination of checkboxes clicked. Everything is working fine except one case:
I'm opening additional telerik radwindow which has some validation within its own validation group. If I close the window with any validation error, then the first click on any of that checkboxes
does not do postpack.
Only FIRST click is not working. next time I click everything is working fine. I've tried to add check boxes to theirs own validation group and change casing validation to true. But behaviour is exactly the same. When I add test javascript method to onclick for that checkboxes then this method is fired without issue. There is just no postback.
I also tried to reset all validators for window validation group on window close, they are reset, but behaviour is not checking, and first click is not working.
UPDATE:
I've made one more check and realized it only happens if I do in javascript:
window.Page_ClientValidate("MyGroup")
Which I have to do before do some calculations on popup window.
During dynamic validators, when updating validatable controls eveything is working properly, even if validation fails. So the problem is *Page_ClientValidate*
I know this is an old question but I ran into almost the exact issue and thanks to the following link I was able to resolve the issue:
Issue with postbacks and clicking on controls twice
I ended up adding this code to my code behind to add a javascript event handler for the checkbox's click event:
Checkbox1.Attributes.Add("onclick", "checkboxChanged();");
I then added the following javascript:
function checkboxChanged(arg1) {
Page_BlockSubmit = false;
}
The checkbox in my situation was also set to have CausesValidation set to false but I don't think that's required for this fix. Hopefully this helps someone.

CustomValidator ServerValidate method does not fire

I've put a CustomValidator on my form. I have not set its ControlToValidate property. In its ServerValidate event I've written the following:
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid = false;
}
I put a breakpoint to this method but it seems to never come to that point. But if I do this on another form it works like a charm.
The ValidationGroup property of both the button and the CustomValidator are the same
I tried deleting this property in both the button and the CustomValidator, still does not work.
It seems as there's something formwide. I just put a CustomValidator on the form and do not touch any of its properties other than just setting its ServerValidate event method.
EDIT: Here's the aspx part:
<asp:CustomValidator ID="CustomValidator2" runat="server"
ErrorMessage="This is a test"
onservervalidate="CustomValidator1_ServerValidate"
ValidationGroup="PA"></asp:CustomValidator>
<asp:Button ID="btnPensionersOK" runat="server" Text="OK" Width="75px"
onclick="Button1_Click" ValidationGroup="PA" />
Try to force the validation in the button-click handler via Page.Validate:
protected void Button1_Click(Object sender, EventArgs e)
{
Page.Validate();
if(Page.IsValid)
{
// servervalidate should have been called
}
}
Edit(from comments):
If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set ValidateEmptyText to true. You also might want to let the CustomValidator replace the RequiredFieldValidators.
I assume that the validator-order on the aspx decides whether or not a customvalidator's severvalidate is called if a previous Validator already has made Page.IsValid=false. Or ASP.NET is so smart that it assumes the SeverValidate to be costlier than a simple text-is-empty check.
I would also like to put some more help for those who will use CustomValidators and RequiredFieldValidators at the same time. One should take into account that Client side validation takes place first. And the server side validation will occur only after PostBack. I'm sure you got it but just in case this is not quite clear: It means first all the controls that are bound to certain client side working validators must be valid to let Postback to occur. After Page. IsValid is True server side stuff takes place and posts back any changes which includes server side validation messages.
So here are the ways one can make both CustomVCalidators and other built in validators to work at the same time.:
Set both groups of validators to work on Client side. In this case we must ensure that for the custom valitor(s) we spacify the script that will make validation on the client side. Without writing script and just filling in the ServerValidate method the validation will take place in the server.Even if EnableClientScript property is set to True.
Set both groups of validators to work on server side. To do so simply set EnableClientScript to False. But note that this will load the server.

Display a message if client validation fails?

I'm working on an application form for a website which implements ASP.NET validation (including client side).
I have a requirement to display a message at the bottom of the page if the client validation fails. Something along the lines of "Please go back and check your answers".
The problem is, the submit button's OnClientClick event obviously fires before the client validation.
Any idea how I can get around this problem?
On your RequiredFieldValidator if you use both the ErrorMessage and Text attributes the value of ErrorMessage will appear in the ValidationSummary, while the value of Text will appear at the location of the RequiredFieldValidator.
E.g.:
<RequiredFieldValidator
ErrorMessage="This will appear in the summary at the bottom of the page."
Text="This will appear in the middle of the page."
.../>
So to hide the ErrorMessage from the summary simply set it to an empty string:
ErrorMessage=""
Have you set the CausesValidation property on your button?
What is your OnClientClick method doing?
As I understand it you are trying to reinvent the wheel by implementing you own functionality to display the validation message. You should use a ValidationSummary control to display the message to the user. And you should also remove your custom OnClientClick function.

How to not show BaseValidator.Text when using a ValidationSummary in ASP.NET

I'm using a bunch of different asp.net validation controls on a web form. Some of these have their Text property set to things like "* Error" or "You missed these fields".
However, a few of the CustomValidator controls have blank Text properties. I left them blank on purpose because I'm adding the ErrorMessage dynamically depending on which case fails. (A CustomValidator may have many different conditions upon which I set args.IsValid = false)
When an error occurs, the ErrorMessage property that I set is shown both in the ValidationSummary and inside the Validator control. I don't want that. I want to be able to just show the ErrorMessage in the ValidationSummary and not in the BaseValidator.Text property.
My first try was to set the Text property to be a space " ". That didn't work.
What I implemented (for now) is a period that is shown as the same color of the background. It's a hack - and I don't like it. Heck, maybe that's why I'm here!
Here's the code:
<asp:CustomValidator ID="StackOverflowValidator" runat="server"
Text="."
CssClass="validatorstyle"
Display="Dynamic"
OnServerValidate="validate_AllowedToDoSomething"
ValidationGroup="MainGroup" />
<asp:ValidationSummary ID="mainGroupValidationSummary" runat="server"
ValidationGroup="MainGroup"
DisplayMode="BulletList"
HeaderText="There was an error in saving. Please check the following:" />
Inside validate_AllowedToDoSomething I call:
StackOverflowValidator.ErrorMessage = "Custom Error Message #1";
args.IsValid = false;
return;
What I get is "Custom Error Message #1" twice on the web form. Thanks in advance!
Just set display="none" instead of "dynamic" on the BaseValidator, and that should solve it.

Categories