Validation control doesn't prevent PostBack request to server - asp.net

I have big web page, among other there is TextBox and RequiredRangeValidator.
<asp:textbox id="tbNewSvrLic" runat="server" Text="0" BackColor="#FFFFA0" />
<asp:RangeValidator id="NewSvrLicValidator" ControlToValidate="tbNewSvrLic"
runat="server" Type="Integer" MinimumValue="0" MaximumValue="5"
errormessage="Invalid value specified for Additional Server Licenses"
Display="Dynamic" />
<asp:button id="btnOrderSummary" OnClick="btnOrderSummary_Click"
runat="server" text="Display" Visible="False" />
These control are non visible when page is loaded 1st time, but became visible after user do some actions on page and submit some data to server (there Visible status will be changed to true).
When invalid data (for example 10 value) is entered into 'tbNewSvrLic' control then required error message is displayed, but 'Display' button is still clickable and does post-back request to server.
Web site is working under .NET 4.0, very similar functionality is implemented for .NET 3.5, but works as expected...
Please advise. Any thoughts are welcome.
P.S. Probably I need to mention that error message (on validation controls) are displayed in black, not in red as usual...

Can you try using ValidationGroups as mentioned here ?

Related

No KeyDown event on my VB.NET Webform

I'm new to VB.NET webform development, but an old VB/Access developer.
I've used Keydown, Keypress events before in my normal development but cannot find this event with this new web development project I'm starting.
I'm assuming it's something to do with the fact it's a web-form. However when I search I can't find others with this issue so thought I'd ask it here. Below is a screen-shot of the events I have on the text control on the web-form where I'm trying to put the keypress event.
(I wanted to attach my picture showing you the events in the list but I don't have 10 reputation points so won't let me include it).
Is this event not available for web-form development? Essentially what I want to do is have the page check that there is text is both the txtUsername and txtPassword controls before enabling the "Log In" command button.
All I have in the drop-down list for the control is:
(Declarations)
DataBinding
Disposed
Init
Load
PreRender
TextChanged
Unload
Consider using a RequiredFieldValidator:
<asp:TextBox id="Foo" runat="server"/>
<asp:RequiredFieldValidator id="Bar"
ControlToValidate="Foo"
Display="Static"
ErrorMessage="*"
runat="server"/>
And in your submit button's click handler:
If Page.IsValid Then
...
Else
...
End If
You will probably also want to use the HTML5 required attribute:
<asp:TextBox id="Foo" runat="server" required="required" />
You might also consider using aria-required:
<asp:TextBox id="Foo" runat="server" required="required" aria-required="true" />

.net ascx control not retaining value on postback (mojoPortal)

I'm making a custom module for mojoPortal CMS which needs to allow the client to add an affiliate into the database. As far as I can tell, this requires creating a .ascx file and then installing that using the administration toolbar in the Web interface to get it to a point where I can put it into a page, as http://www.mojoportal.com/hello-world-developer-quick-start.aspx.
The form is simple enough, but the values in the text boxes just stay empty when I submit, though the file upload works fine. The code:
<asp:Label ID="Label1" runat="server" Text="Company Name" AssociatedControlID="CompanyName">
</asp:Label>
<asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Company Description" AssociatedControlID="CompanyDescription"></asp:Label>
<asp:TextBox ID="CompanyDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Company Logo" AssociatedControlID="CompanyLogo"></asp:Label>
<asp:FileUpload ID="CompanyLogo" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Add Affiliate" />
EnableViewState for the page and the controls is enabled
The text box is not set to ReadOnly, and there is no funky JavaScript dynamically modifying elements (at least, I didn't set any).
I can work around this by using HTML elements, and get the values using Request.Form. The information is actually there, I can see it in the Request.Form, but I would have to get that by something like Request.Form[CompanyName.ClientId.Replace("_","$")] or Request.Form[6] which both seem very messy and IIRC aren't really the way things are supposed to roll in .NET. Besides, having worked until 3 last night, I really want to know what the answer is now!
Any thoughts anyone?
What I had done was not created a click event for the button, relying on the fact that it was posting to the server (like I would in PHP). Oops! When I added a click event, then the text boxes retained their value when I was within that method.

RequiredFieldValidator have to click twice

I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.

Form Validators firing on Umbraco

We are developing a web application in Umbraco 4 and have come across an intermittent problem when posting data between pages. When a form submission is posted to a new page all of the validators are firing causing various unwanted results, we have tried various posting methods using different buttons with the same result. Has anyone else come across this issue?
The validation group is working but I got to the bottom of the issue.
One of the submit buttons on the page was a html submit, with no runat server, so the code-behind didn't know where the submit had come from, so all validations where fired, regardless of validation group
All postbacks events, unless otherwise specified, will cause all validators on a page to be triggered. Either...
A) Set the ValidationGroup property on each of the validators as well as the control which you do want to trigger validators to the same name.
<asp:RequiredFieldValidator ID="valName" runat="server" AssociatedControlID="txtName" ValidationGroup="AllRequired" />
<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="AllRequired" OnClick="..." />
<asp:Button ID="btnSkip" runat="server" OnClick="..." />
In this case, btnSkip won't trigger the validators.
B) Set the CausesValidation property on the control which you don't want to trigger validation to false.
<asp:Button ID="btnSubmit" runat="server" CausesValidation="false" OnClick="..." />

Validation summary control not showing message?

I have a page with TextBox to enter a Mobile Number.
For that I have validated it using RequiredFieldValidator and RegularExpressionValidator
with display=none:
And also I have placed a ValidationSummary Control
<asp:ValidationSummary ValidationGroup="mobile" ShowSummary="false" ID="vsValid"
runat="server" ShowMessageBox="true" Enabled="true"
DisplayMode="SingleParagraph" />
<asp:TextBox ID="txtMobileNumber" runat="server" CssClass="Qinputbox"></asp:TextBox>
<asp:RequiredFieldValidator Display="None" ControlToValidate="txtMobileNumber"
ID="reqValidMobileNo" runat="server" ErrorMessage="*"
ValidationGroup="mobile"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate="txtMobileNumber" Display="None"
ID="regExValidMobileNo" runat="server"
ErrorMessage="Please enter a valid mobile number."
ValidationExpression="^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9]
(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$"
ValidationGroup="mobile"></asp:RegularExpressionValidator>
when I entered characters in the TextBox it is not showing the summary.
What might be the problem?
From you code fragment:
ShowSummary="false" Looks like a problem.
You should set either ShowSummary="false" to true or EnableClientScript="true". I'm assuming that you want the latter because you have ShowMessageBox="true".
Have a look: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.enableclientscript%28v=vs.71%29.aspx
Use this property to specify whether
the ValidationSummary control updates
itself using client-side script. When
set to true, client-side script is
rendered on the client to update the
ValidationSummary control, if the
browser supports that feature. When
set to false, no client-side script is
rendered on the client and the
ValidationSummary control only updates
itself on round-trips to the server.
In this situation, the ShowMessageBox
property has no effect.
In web.config setting:
<httpRuntime targetFramework="4.5"/>
This setting turns off Javascript injection of validation (client side).
Remove this and retry.
Ensure that you do NOT have this in the web.config file:
<xhtmlConformance mode="Legacy"/>

Resources