Issue with Required Field validation in a asp.net application having two pages workflow - asp.net

I have a asp.net application with two pages. The first page is having a next button and second page is having a back and submit button. The second page contains text box control with required field validtor.
As per current design, required field validator on 2nd page should get fired on clicking on submit button.
The issue I am having is :- User Fills the first page and then go to second page. Now, user wants to comeback to first page and then go to second page, the required field validator on text box is getting fired and showing up the error message. I dont want this. This should be like - text box should be blank and no error message should be displayed. Error messages should be displayed on clicking on the Submit button on the second page.
I tried couple of options :- 1. putting InitialValue on required field etc.. but no luck.
Could you pls help me out??

The following Markup should solve your problem :
<asp:TextBox ID="tbData" runat="server" ValidationGroup="Submit"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="Submit" />
<asp:Button ID="btnBack" runat="server" Text="Button" />
<asp:RequiredFieldValidator ID="reqFieldVal1" runat="server" ErrorMessage="Field must be filled in."
ControlToValidate="tbData" ValidationGroup="Submit"></asp:RequiredFieldValidator>
These fields use the ValidationGroup property to set which fields will cause the validation of which validator controls.
You will notice that there is no ValidationGroup property on the btnBack button as this should not cause validation.
For more information on this see the MSDN Article for Specifying Validation Groups

Related

Avoid double clicking submit with validator display set to dynamic

I have some ASP.NET CustomValidators with Display="Dynamic". The layout of the page requires that I use them like this without a ValidationSummary.
<asp:CustomValidator ID="vldFirstThree" ControlToValidate="txtFirstThree" runat="server" OnServerValidate="ValidateTextBox" ValidateEmptyText="true" ErrorMessage="Please enter the first three characters of the owner name.<br />" CssClass="error" Display="Dynamic" Enabled="true"/>
If the user leaves the field blank and clicks submit, the error text is displayed. If the user fixes the problem and then clicks submit again, the validation error text clears, but a second click is required to submit the page. I've tried writing some server-side code and some JavaScript to cause the validated TextBox to blur() before submitting, but I'm still running into the need to click twice: once to clear the message and again to submit.
Is there a way to clear the error text and perform the submit with one click?
The answer is the second one here: RequiredFieldValidator have to click twice.
I had to set EnableClientScript="false".

Requiredfieldvalidtor in asp.net stops me from going back?

When a user clicks a link, he's sent to a register form. In this form I have certain fields that need to be filled in. So I use required field validator on them.
On the page there is also a Back button, incase the user doesn't want to register. But when you press back, it says you need to fill in the fields or you won't go back.
Any suggestions on how this can be resolved?
Set the CausesValidation property on the back button to false:
<asp:Button id="btnBack" runat="server" CausesValidation="false" OnClick="btnBack_Click" Text="Back" />

2 user controls on registered on one aspx page not validating validation properly

I have 2 user controls on registered on one aspx page.
UserControl1 us having one text box with require field and one submit button.
UserControl2 is also having one text box with requirefiled and save button.
Expected o/p is-
When I am clicking on any button out of 2(submit or save). Then only related text boxof that user control should be validate.
But the error is
Both text boxes are validate.
Please help me .
Set the ValidationGroup properties to limit which fields get validated when the buttons are pressed.
So for example, if these were contained within the first user control:
<asp:requiredfieldvalidator id="NameValidator"
controltovalidate="NameTextBox"
validationgroup="UserControlOne"
errormessage="required"
runat="Server" />
<asp:button id="Submit"
text="Submit"
causesvalidation="true"
validationgroup="UserControlOne"
runat="Server" />
Clicking the "Submit" button would only cause the validators that have UserControlOne specified as the ValidationGroup to validate.
Edit: When you call Page.Validate() you are validating every group on the page. Call the overloaded Page.Validate(validationGroup) to validate a specific one. e.g. Page.Validate("UserControlOne")

On [Enter] keyup of textbox incorrect validation group being fired

I have two validation groups on a form (we will call them VG1 & VG2). I have the following code:
<asp:TextBox ID="textbox1" runat="server" ValidationGroup="VG2" />
<asp:RequiredFieldValidator Text="*" ForeColor="#C301B9" ID="RequiredFieldValidator1" runat="server" ErrorMessage="My error message" ControlToValidate="textbox1" ValidationGroup="VG2" />
When this control has focus and I hit enter the validation summary displays the validation error messages for VG1.
VG1 fields are not visible (set via JS). I think I may need to also disable VG1 validation group summary.
Any help with this would be appreciated.
Hitting enter when in the text box is probably submitting the form or doing the equivelent of clicking some button that is in the VG1 validation group. If you put everything in VG2 within a Panel control and set the DefaultButton property on the panel to be a button in VG2 then hitting enter in the text box will no longer fire the validators in VG1. If you are simply hiding controls vai javascript remember they are still there on the page and their actions can still be triggered.

Can't change an asp page because of a required validator

On one of my asp.net pages I have a few textboxes and a required validator attached to them. Everything is ok with validation, but when I want to change page to another required validator doesn't allow it because I don't fill in a textbox :/
How to allowed change page without fill a validate textbox? I must create a condition in "exit page" (how is it named ?) event disabled required validation?
Please help ;)
If you're using some control to move to the next page, set it's CausesValidation property to False.
For example, if you're clicking a button that moves you to the next page, it would be like:
<asp:LinkButton id="myButton" runat="server" CausesValidation="False" .... />

Resources