requiredfield validator is preventing another form from submitting - asp.net

I have a page with many forms in panels and usercontrols, and a requiredfield validator I just added to one form is preventing all of my other forms from submitting. what's the rule that I'm not following?

Are you using ValidationGroups? Try assigning each control with a validation group as well as the validator that you want to use. Something like:
<asp:TextBox ID="txt1" ValidationGroup="Group1" ruant="server" />
<asp:RequiredFieldValidator ID="rfv1" ... ValidationGroup="Group1" />
Note, if a button doesn't specify a validation group it will validate all controls that aren't assigned to a validation group.

You should be setting ValidationGroup property to a different value for each group of elements. Your validator's ValidationGroup must only be same with the control that submit its form.

Related

Do i need to call Page.IsValid explicitly if i am using Asp.net valdiators?

I have used Asp.net validation controls like Required Field Validator etc so do i need to explicitly mention Page.IsValid or is it called by default ?
<label>DeadLine</label>
<asp:TextBox ID="txtDeadLine" runat="server" CssClass="textField_width"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="txtDeadLine" ErrorMessage="Deadline Date is Required"
ForeColor="Red" ValidationGroup="GroupComposeLetter">Deadline Date is Required
</asp:RequiredFieldValidator>
<br />
<%--<asp:GridView ID="gridViewComplaints" runat="server"
></asp:GridView>--%>
<br />
If the control has CausesValidation set to true (default) it is not needed.
Controls where this is set by default:
Button,
ImageButton,
LinkButton
Web server controls,
HtmlInputButton,
HtmlInputImage,
HtmlButton
HTML server controls,
controls that can automatically post back to the server such as the TextBox, CheckBox, ListControl, and BulletedList
So if you've set it to false you could force validation on serverside by calling Page.Validate(ValidationGroupName) manually. Afterwards you can check Page.IsValid.
Validation occurs after Page_Load, but before event handlers (See http://msdn.microsoft.com/en-us/library/ms178472(v=VS.100).aspx).
If your button does not cause validation, you must manually fire Page.Validate.
(Page.Validate method is fired automatically by controls that have the CausesValidation property set to true(Which is default value for Button control).
Page.IsValid property tells you whether the validation succeeded or not.)
More details, please see this discussion
How does Page.IsValid work?

Different buttons in the same form

I have an asp form.it contains 3 menu item and save button under each menu[under each menu there are different fields]. I am doing validation using Ajax validators. My problem is after filling all mandatory fields for the first menu and on clicking the button the page is not post backing.Because i have some other fields mandatory for another menu.How can i solve this?
Try using Validation Groups
Validation groups allow you to organize validation controls on a page
as a set. Each validation group can perform validation independently
from other validation groups on the page. You create a validation
group by setting the ValidationGroup property to the same name (a
string) for all the controls you want to group. You can assign any
name to a validation group, but you must use the same name for all
members of the group.
Basically you need to assign a validation group name for each set of controls you wish to validate and then assign the same validation group name to the submit button associated with that group.
From the above link:
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation="true"
validationgroup="PersonalInfoGroup"
runat="Server" />
You need to use ValidationGroup property of Validation controls of ASP.Net.
Please provide the group name to each group of controls.
Please refer this link
Validation of fields will only be done when you mention the validation Group for the fields and in click event of the button. As you have other 2 menu's with mandatory fields with out any validation group name the button click event is trying to validate all the fields and not posting back. Give Validation Group names as A, B & C then try to validate...All the best

Is it possible to apply a particular CSS to a textbox in case it cannot pass a RequiredFieldValidator?

Is it possible in ASP.NET to enforce a Requiredfieldvalidator control to add a CssClass to a TextBox in case it fails the validation?
<asp:TextBox ID="txtSomeInput" runat="server" />
<asp:RequiredFieldValidator runat="server" Text="*" ErrorMessage="This is required." ControlToValidate="txtSomeInput"
<!-- Do something so that if validation fails, add CssClass 'failed' to txtSomeInput; possible? -->
/>
You can achieve that using CustomValidator with code-behind validation logic using CssClass property.
Or inherit RequiredFieldValidator and extend validation failure reaction
A possible solution would be to use this example.
It relies on altering and using some of the javascript code generated by asp validation controls and has the advantage of being usable with all kinds of validation controls and does not require a post back (if client validation fails no postback is made to alter the class of the text box).
best of luck,

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")

2 DetailsView validation conflict problem

I have 2 DetailsView.
The first one is hidden (display:none) with DefaultMode=Insert, has RequiredFieldValidator
<asp:RequiredFieldValidator ID="valRequireAddedBy" runat="server"
ControlToValidate="txtAddedBy" SetFocusOnError="true"
Text="Your name is required." Display="Dynamic">
</asp:RequiredFieldValidator>
The second one, in edit mode.
When I try to submit it I get an error:
htmlfile: Can't move focus to the
control because it is invisible, not
enabled, or of a type that does not
accept the focus.
Only if i remove the validator from first DetailsView it works.
Can you use the ValidationGroup property on the validators to create two groups, one for each DetailsView? I'm not sure how you're firing the event that causes validation in each case, so I don't know if you can associate a ValidationGroup with the firing control or not...
In your second DetailsView, set CausesValidation property to false
That way your "EditMode" details view won't cause the validation to fire
I guess DetailsView does not expose CausesValidation property, you'd have to deal with it on your DetailsView.UpdateItem event

Resources