2 DetailsView validation conflict problem - asp.net

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

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?

Disable Validation controls in asp.net pages

My asp.net page has multiple text-boxes and DropDownLists that all have required-field validators.
My site also has multiple pages that you can move between with the click of a button. (All pages have all the buttons) When a page loads and decide I want to go to a different page without entering information into the form, I click a button to move to a separate page and the validation pops up and I can't change the page, it stops me every time?
Any ideas on how to stop this?
I know you already found a solution, but I just wanted to throw in another method for people to see.
When using validators it is common practice to use ValidationGroups. When a validator belongs to a ValidationGroup, it is only triggered by another control in the validation group. For example:
<asp:TextBox ID="NameBox" runat="server"/>
<asp:RequiredFieldValidator ID="NameVal" ControlToValidate="NameBox"
ValidationGroup="ValSubmit" runat="server"/>
//...More input fields and validators...
<asp:Button ID="SubmitButton" ValidationGroup="ValSubmit" runat="server"/>
Using this method any validators with the "ValSubmit" ValidationGroup will be triggered only by the SubmitButton and not by other controls. Now you don't have to put CausesValidation="false" on EVERYTHING else that causes a postback.
As everyone else had mentioned, set the CausesValidation property to false.
<asp:Button ID="MyButton" Text="Go Back" CausesValidation="False" />
This is straight from the Microsoft Help Page:
By default, page validation is performed when a Button control is clicked. Page validation determines whether the input controls associated with a validation control on the page all pass the validation rules specified by the validation control.
You can specify or determine whether validation is performed on both the client and the server when a Button control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.
Note:
You should set the CausesValidation property to false when you are using the PostBackUrl property to post back to a different page. You should explicitly check validation when posting back to a different page. For an example, see the Remarks section of the PostBackUrl property.
This property is commonly set to false for a reset or clear button to prevent validation from being performed when the button is clicked.
When the value of the CausesValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group for which the Button control causes validation.
set property CausesValidation = "false" to the buttons where you don't want to trigger validation
CausesValidation="false" on the button ;)
<asp:Button CausesValidation="True|False" />
Gets or sets a value indicating whether validation is performed when the Button control is clicked.
read more about Button.CausesValidation Property

Two ASP.net buttons on same page problem?

I have two asp buttons on a single page. In the following format
textbox1
required field validator1
button1
textbox2
required filed validator2
button2
if I keep cause validation on both buttons true, then its not firing the server side click event, but I am able to validate these text boxes using required field validators.
If I keep this cause validation on both buttons false, its firing the server side click event, but I am not able to validate these textboxes using required filed validators
Can you please tell me the best solution
Thanks
CausesValidation should be true on both.
What you need to do is set ValidationGroup Button1Validation on the textbox1 validator and button1. Set a ValidationGroup of Button2Validation on the textbox2 validator and button2.
Validation groups separate validation rules into groups so that the groups don't overlap and interfere with each other.
The default behavior of the RequiredFieldValidator is that when you don't insert something into the textbox to which it's coupled, and client side validation is turned on (defaults to true), then it won't posback and as such the server side event never gets hit.
You can turn off the client side validation on the validator control by setting the property EnableClientScript to false:
<asp:RequiredFieldValidator ID="TextBoxRequiredValidator"
ControlToValidate="NumberTextBox"
EnableClientScript="False"
Display="Dynamic"
ErrorMessage="Please enter a value."
Text="*"
runat="server"/>
Taken from the MSDN documentation.

Using Validation controls with a GridView

A typical situation:
In my GridView control, I have a Footer row which contains a Textbox and an "Add" Button. When the button is pushed, the Text entered in the TextBox is added to the grid. I also have a validation control to require that, when the button is pushed, that text has been entered in the TextBox. After a new row is added, the textbox is clear to allow for easy entry of the next item.
The user may also edit the text in previously entered rows by clicking the Edit LinkButton, which puts the row into edit mode. Clicking an Update LinkButton commits the change.
The problem:
When, I click the Update link to commit the changes, if text has not been entered in the Footer row's TextBox (the row used to add a new entry), the validation control returns a "Entry Required" error. It should only require an entry if the Add button is pushed, not if the Update LinkButton is pushed.
It seems that the server side Validation control's validating event fires before the GridView's RowCommand event or the btnAdd_Click event, so I am wondering how, from the server, I can determine what event fired the postback so I can determine whether what edits should be performed for the given situation.
I am using a mix of client side "required" validation edits as well as more complex server sides. Since I probably have to have some server sided validations, I would be happy with just knowing how to handle server sided validations, but really, know how to handle this situation for client validations would also be helpful.
Thanks.
Convert your CommandField into a TemplateField, and in the EditItemTemplate, change the Update LinkButton's CausesValidation property to false.
Update:
Converting to a TemplateField is simple and doesn't require any code changes (just markup):
Changing the CausesValidation property to false in the markup is also straightforward:
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton>
<%--
More controls
--%>
</EditItemTemplate>
<ItemTemplate>
<%--
Controls
--%>
</ItemTemplate>
</asp:TemplateField>
Now, if you want your footer and data rows to be validated separately, you need to use validation groups, which is explained in Microsoft's documentation. All the controls in the same validation group will have their ValidationGroup property set to the same value, like this:
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" ValidationGroup="GridViewDataRowGroup">
</asp:LinkButton>

requiredfield validator is preventing another form from submitting

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.

Resources