asp.net: Postback disabled after validation failed - asp.net

After validation failed, I have a problem with controls (dropdownlist or button) that should cause a new postback. I will try to explain it clearly...
The purpose of my page is to save fives dates in a database. The page has the following controls:
Five textboxes, each one containing a date
A reset button (CausesValidation=false) to restore a default date in one of the 5 textboxes
A dropdown list (AutoPostback=true, CausesValidation=false) of predefined templates that applies 5 dates to the 5 textboxes
A button to save the dates to the database
The textboxes can be edited manually. So, when I click the Save button, if the format of the dates is not valid, the validation fails and the save is aborted. The problem is just after that. If I click on the Reset button or select an item in the dropdownlist, the postback is not triggered. If I try again, then it works. Is there a way to make it work the first time after the first validation failed? I tried deactivating the validation on the client-side when changing the selection in the dropdownlist but the postback still does not occur.
Here is the relevant part of the code:
<asp:DropDownList ID="cboScheduleTemplates" runat="server" AutoPostBack="true" CausesValidation="false" />
<asp:TextBox ID="txtDateDelivery1" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery1" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery1" Text="*" />
<asp:TextBox ID="txtDateYearbookQuantity" runat="server" />
<asp:RegularExpressionValidator ID="revDateYearbookQuantity" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateYearbookQuantity" Text="*" />
<asp:TextBox ID="txtDateDelivery2" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery2" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery2" Text="*" />
<asp:TextBox ID="txtDatePersonalizations" runat="server" />
<asp:RegularExpressionValidator ID="revDatePersonalizations" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDatePersonalizations" Text="*" />
<asp:TextBox ID="txtDateDelivery3" runat="server" />
<asp:Button ID="btnSetDefaultDelivery3" runat="server" ValidationGroup="Schedule" CausesValidation="false" />
<asp:RegularExpressionValidator ID="revDateDelivery3" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery3" Text="*" />
<asp:Button ID="btnSaveSchedule" runat="server" CssClass="btnAction" Text="Save" ValidationGroup="Schedule" />
<asp:ValidationSummary ID="validationSummarySchedule" runat="server" ValidationGroup="Schedule" DisplayMode="List" />

As suggested in this post, the problem comes from calls to Page_ClientValidate. So I wrapped the client function like this and the problem went away:
function DoPageClientValidate(validationGroupName)
{
var result = Page_ClientValidate(validationGroupName);
Page_BlockSubmit = false;
return result;
}

Related

ASP .NET CompareValidator control locks everything

I have a form with a field that has a asp:CompareValidator control which checks if the text in the input is of type Integer.
The problem is that the form has some other buttons to navigate to other pages, so when the CompareValidator triggers it locks those buttons until the input in the controled field is Integer although it's not necessary to navigate.
Here's the code:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>
Is this the expected behaviour of the asp:CompareValidator control?
You can specify a ValidationGroup in your button and in your CompareValidator. Then the CompareValidator will be used only for the corresponding ValidationGroup:
<asp:TextBox runat="server" ID="txtRelacion" placeholder="Id del evento"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtRelacion" ValidationGroup="form" CssClass="compare-validation-error" />
<asp:Button runat="server" ID="submitButton" ValidationGroup="form" Text="Submit Form"/>
<asp:Button runat="server" ID="navigateButton" Text="Navigate somewhere"/>

Required Field Validator on FileUpload Control

I have a form in which I have two textboxes and one fileupload control, I am using required field validator on one textbox and on Fileupload control, When I am clicking the submit button, its disabling the fileupload control and not showing any validation for it.
I also have second button for Cancel, clicking on which redirects to previous page, when I am clicking this button its also disabling the fileuploadcontrol.
Below is my code
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="validate" runat="server"
ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtSubj" runat="server" ></asp:TextBox><asp:RequiredFieldValidator
ID="rfvSubject" ControlToValidate="txtSubj" runat="server" Display="Dynamic"
EnableClientScript="true" ErrorMessage="* required" ValidationGroup="validate" />
<asp:Button ID="btnupload" runat="server" Text="Send" ValidationGroup="validate"
OnClick="btnupload_Click">
<asp:Button ID="btncancel" runat="server" Text="Cancel" OnClick="btncancel_Click"
/>
Try using no ValidadationGroup:
<asp:FileUpload ID="fupDocument" runat="server" Width="100%" />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="* Required Field" ControlToValidate="fupDocument">
</asp:RequiredFieldValidator>
I tried and work it.

asp.Net ValidationGroup validating correctly except on Enter key

I've seen a variety of questions here that are very close to this issue but none which seem to fit this specific scenario.
Description:
I have ID and Password fields with a Log On button in the Master page. These are in the "LoginValidationGroup" and they work fine.
Now, in the Default.aspx, I have an email TextBox and submit button that are in the "NotifyMeValidation" group, and they also work, BUT not if you hit the enter key rather than the submit button. The Submit button works fine - Enter key ... not so much.
The Enter key causes the validation to occur on the LoginValidationGroup, even though the TextBox is set to CausesValidation="true", and it is in the NotifyMeValidation group.
I guarantee people are going to enter an email in that box and hit Enter. I would!!! And when they do, they're going to see a callout balloon at the top telling them the User ID is required.
What's my error here? If I need to post the actual code I can do so.
Actually, let me just go ahead and do that.
From the Default.aspx:
<asp:RequiredFieldValidator
runat="server"
ValidationGroup="NotifyMeValidation"
ID="EmailRequiredValidator"
ControlToValidate="SenderEmailTextBox"
ErrorMessage="Email is Required"
Display="None" />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
PopupPosition="Left"
ID="EmailRequiredValidatorExtender"
Width="185px"
TargetControlID="EmailRequiredValidator"
WarningIconImageUrl="/Images/warning.gif" />
<asp:Label ID="SenderEmailLabel" runat="server" ssociatedControlID="SenderEmailTextBox" EnableViewState="false" Text="Email:"></asp:Label>
<asp:TextBox ID="SenderEmailTextBox" runat="server" ValidationGroup="NotifyMeValidation" Width="350" BackColor="#cccccc" CausesValidation="true"></asp:TextBox>
<br /><br />
<asp:Button ID="SubmitButton" runat="server" ValidationGroup="NotifyMeValidation" EnableViewState="false" CssClass="submit" Text="Send" CausesValidation="true" OnClick="btnSubmit_Click" />
From the Master page:
<asp:Label CssClass="LoginHeading" ID="UserNameLabel" runat="server" AssociatedControlID="UserNameTextbox">UserName: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="UserNameTextbox" runat="server" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Label CssClass="LoginHeading" ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextbox">Password: </asp:Label>
<asp:TextBox CssClass="LoginTextBox" ID="PasswordTextBox" runat="server" TextMode="Password" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>
<asp:Button CssClass="LoginHeading" ID="LoginButton" runat="server" Text="Button" CommandName="Login" ValidationGroup="LoginValidation" CausesValidation="True" onclick="LoginButton_Click" />
And the Master page validators...
<asp:RequiredFieldValidator runat="server" ID="UIDRequired"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A User ID is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="UIDValidationExtender"
PopupPosition="Left"
Width="185px"
TargetControlID="UIDRequired"
WarningIconImageUrl="/Images/warning.gif" />
<asp:RequiredFieldValidator runat="server" ID="PWRequired"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
Display="None"
ErrorMessage="<b>Required Field Missing</b><br />A password is required." />
<ajaxToolkit:ValidatorCalloutExtender
runat="Server"
ID="PWValidationExtender"
PopupPosition="Left"
TargetControlID="PWRequired"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
Display="None"
ValidationGroup="LoginValidation"
ID="CustomUserExistsValidator"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Invalid UserID!</b><br />User ID doesn't exist. Please try again.<br />"
OnServerValidate="CustomCheckUserExists"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserIDCalloutExtender"
TargetControlID="CustomUserExistsValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomPWValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="PasswordTextbox"
ErrorMessage="<b>Invalid Password!</b><br />Please try again.<br />"
OnServerValidate="CustomValidatePassword"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="PWCalloutExtender"
TargetControlID="CustomPWValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
<asp:CustomValidator
runat="server"
ID="CustomUserApprovedValidator"
Display="None"
ValidationGroup="LoginValidation"
ControlToValidate="UserNameTextbox"
ErrorMessage="<b>Approval Error!</b><br />This UserID exists, but is not yet approved.<br />"
OnServerValidate="CustomCheckUserApproved"/>
<ajaxToolkit:ValidatorCalloutExtender
runat="server"
PopupPosition="Left"
ID="UserApprovedCalloutExtender"
TargetControlID="CustomUserApprovedValidator"
Width="185px"
WarningIconImageUrl="/Images/warning.gif" />
Try adding an <asp:Panel> around each set of controls and setting the DefaultButton property to the ID of the button you want to click when the users hits Enter.
Just to explain what is happening here, the aspx's default button is being fired when you press enter on the textbox. To handle this you could you could fire some jQuery code when the enter key is clicked on the textbox, this code will then perform the click on the specific button that you require. Check this

Validating controls in ListView insert/edit templates

I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.

Two Validation Groups in a single button without client click event

In a multigrid I am validating two controls like date and amount. It is validating correctly when I press the tabevent. When I press the save button it is not validating. I am using two validations groups and two validation summary. Then in save button Ialso tried onclientclick() function with javascript it is working fine but if I give the correct value in date and amount records, it is not saving. how to over come this.
Date
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="txtDate" Text='<%# Bind("AD_REF_DATE") %>' runat="server" CausesValidation="true"
ValidationGroup="group" Width="80px" AutoPostBack="true" OnTextChanged="txtDate_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CompareValidator ID="dateValidater" runat="server" ControlToValidate="txtDate"
Operator="DataTypeCheck" Type="Date" ValidationGroup="group" EnableClientScript="true"
ErrorMessage="Please enter a valid date (mm/dd/yyyy)." SetFocusOnError="true" Display="None">*</asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
Amount
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="txtAmount" MaxLength="17" Text='<%# Bind("AD_AMOUNT") %>' CausesValidation="true"
ValidationGroup="req" runat="server" AutoPostBack="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RegularExpressionValidator ID="regVal1" runat="server" ControlToValidate="txtAmount"
ErrorMessage="Format(13int,5deci)" ValidationExpression="^[1-9]\d{0,12}(\.\d{1,2})?%?$"
ValidationGroup="req" Display="None" EnableClientScript="true" SetFocusOnError="true">
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
validations summary:
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ValidationGroup="req"
HeaderText="Amount:Invalid Format" DisplayMode="BulletList" ShowMessageBox="true"
ShowSummary="false" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="group"
HeaderText="Date:Invalid Format" DisplayMode="BulletList" ShowMessageBox="true"
ShowSummary="false" />
button save:
<asp:Button ID="ButtonSave" runat="server" CssClass="button" CausesValidation="true" Text="<%$Resources:TJFAS501, ButtonSave %>"
OnClick="ButtonSave_Click" TabIndex="6" />
How to validate this in the button save also two popup box should be shown?
You might want to try firing the validate function for both groups manually via the button's OnClientClick since you have two validation groups you need to validate against. Currently your validation is not firing because you do not have any ValidationGroup assigned to your button so it is just looking for validators with no ValidationGroup defined (yours groups are: group and req).
You can call Page_ClientValidate() via javascript to fire off the validation checks manually (be sure to set CauseValidation on your button to false) and it has an optional parameter that takes the validation group.
<asp:Button ID="yourButton" runat="server" OnClick="ButtonSave_Click"
CausesValidation="false" TabIndex="6"
OnClientClick="return (Page_ClientValidate('group') && Page_ClientValidate('req'));" />
You can read more about Page_ClientValidate on MSDN.
It would be easier to just have one validation group for each action (eg. your button) but I assume you need two groups for some reason.
Try this..
<script type="text/javascript">
function Validate() {
var isValid = false;
isValid = Page_ClientValidate('Group1');
if (isValid) {
isValid = Page_ClientValidate('Group2');
}
return isValid;
}
</script>

Resources