This is the issue I have going right now with validation controls and I simply can't find answer anywhere for it.
I currently have a required field validator watching a listbox. The validator's initial value property is set to "None". The first item in the listbox's value is also set to "None", thus forcing the user to change the selection. However I don't want the validation to show on the selected index change, I want it to show after a button press. So I changed the validation group of the validator and the button to "final" however the validator still shows on selected index changed without the button being pressed.
My code on the validator:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please Select an Incident" ControlToValidate="lstIncidents"
ValidationGroup="final" CssClass="style3" InitialValue="None" Display="Dynamic">
</asp:RequiredFieldValidator>
My Code on the Submit Button:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="final"
Enabled="False" />
Anyone see what might be wrong here?
Thanks in advance!
Set the validator's EnableClientScript to false (default is true):
<asp:RequiredFieldValidator EnableClientScript="False" ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please Select an Incident" ControlToValidate="lstIncidents"
ValidationGroup="final" CssClass="style3" InitialValue="None" Display="Dynamic">
</asp:RequiredFieldValidator>
Related
I have a GridView which has a TextField column in it .. I've setup validation for this TextField column so that it requires an input (i.e. its not optional), and that the input can be a positive integer only ..
The errors do show up when a text field is either empty or has doesn't have a positive integer value, but there's a server-side button which still executes even when there are errors in the GridView ..
I want the button to NOT do its processing if there are inputs errors .. Currently this doesn't happen, as the button's click event is still called even when there are errors ..
GridView Markup Code:
<asp:GridView ID="EPSAndTSRValuesInputGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="EPS Value">
<ItemTemplate>
<asp:TextBox ID="EPSValue" Text='<%# Eval("EPSValue") %>' runat="server" CausesValidation="True" ValidationGroup="Display"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid value for EPS!"
ValidationExpression="^\d*$" ControlToValidate="EPSValue" ValidationGroup="Display"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="EPSValue" ValidationGroup="Display"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Button which performs the next action:
<asp:Button ID="btnDisplayReport2"
runat="server" CssClass="ButtonStyle"
Text="Display Report" ValidationGroup="Display" OnClick="btnDisplayReport2_Click" CausesValidation="true"/>
This is happening because your Button have validation group Display so on click of it will validate only control with same group i.e. Display.As I can see there no validation group of your Textbox so it will not validate it in button click..,to cause validation on Click of Button Add same validation group in your Textbox,RegularExpressionValidator, and RequiredFieldValidator too.
I think it's because they don't have the same ValidationGroup.
Try adding ValidationGroup="Display" to your validators.
You are saying to execute the validation group Display on your button click.It will validate only control have the validation group (dispaly) as you mentioned.You are not defined any validation group for Textbox validators Try by Add same validation group(displa) in your Textbox,RegularExpressionValidator, and RequiredFieldValidator.
I have a required field validator for a textbox which I want to disable on click of a link button. I coded like so.
Protected Sub lnkDeleteTimeSlots_click(sender As Object, e As EventArgs)
txtTimeslotName_RequiredFieldValidator.Enabled = False
End Sub
The design of the textbox.
<asp:TextBox ID="txtTimeslotName" runat="server"></asp:TextBox>
<font color="red">*</font>
<asp:RequiredFieldValidator ID="txtTimeslotName_RequiredFieldValidator"
runat="server"
ErrorMessage="Timeslot Name Required!"
Display="None"
ControlToValidate="txtTimeslotName"
ForeColor="Red"
ValidationGroup="Timetable">
</asp:RequiredFieldValidator>
But it still validates for the required field. What is the issue?
EDIT:
The linkbutton is inside a grid. When it is clicked, I open a popup window with the textbox and a button. The code for the button is like so.
<asp:Button ID="btnMAdd" runat="server" Text="Add Timeslot" PostBackUrl="~/TimeSlots.aspx" OnClick="btnMAdd_Click" CssClass="button" ValidationGroup="Timetable" OnClientClick="javascript:shouldsubmit=true;" />
Set Cause Vlaidation Property of Link Button to falselike this...
CausesValidation="false" so it will allow you to call your code and disable your Validator...
I have fixed this issue - removed the CausesValidation from the linkbutton.
I have several required fields and regex's on my form. The validation is fired on a button click. When the button is clicked, the error messages are showing where the asp:RequiredField are declared and not in the validation summary. Here is my code:
Validation Summary:
<asp:ValidationSummary id="mySummary" DisplayMode="List" HeaderText="Error:" EnableClientScript="true" ShowSummary="true" runat="server" ValidationGroup="valGroup" />
Required Fields/Regex:
<!-- Required -->
<asp:RequiredFieldValidator ID="reqField1" ControlToValidate="txtSomething" ErrorMessage="Something is required" runat="server" Display="Static" InitialValue="" ValidationGroup="valGroup" />
<!-- Regex -->
<asp:RegularExpressionValidator runat="server" id="regexField1" ControlToValidate="txtSomething"
ErrorMessage="Something in the wrong format." ValidationExpression="^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$" ValidationGroup="valGroup">
</asp:RegularExpressionValidator>
Button:
<asp:LinkButton ID="btnValidate" runat="server" CausesValidation="True" ValidationGroup="valGroup" >Validate</asp:LinkButton>
Any suggestions?
It's hard to tell from this, as it does look like it's setup correctly to me. The only way to debug is to check the Page.Validators collection, find the validators in there, and ensure IsValid is false for those guys. All the ValidationSummary does is check this collection, and for any with a matching validation group, if the IsValid property is false, adds it to the list that's rendered out.
I have a panel, controls in the panel..these controls validated with a validation group.
This panel will show / hide when a dropdown box changes.
Question is...i want to validate all the controls with validation group in the panel
how to write the code
You define validationgroup="Test" on your validators , and you define also this validationgroup="Test" on your Button.
Sample
.....
<asp:requiredfieldvalidator id="NameTextBoxRequiredValidator"
controltovalidate="NameTextBox"
display="Dynamic"
text="Please enter your name."
validationgroup="ForPanel" <----------------
runat="server"/>
<asp:button id="SubmitButton"
text="Submit"
validationgroup="ForPanel" <------------------
runat="server"/>
Nota : Gets or sets the name of the validation group to which this validation control belongs.
If have the below markup.
<asp:checkbox id="chkTVLic" runat="server" text="TV Licence" oncheckedchanged="chkDocs_CheckChanged"
autopostback="true" CausesValidation="false" />
<asp:panel id="pnlTVLic" runat="server" visible="false">
<div class="toggle-item-link1 document-date">
<asp:panel id="pnlTVLicIssueDate" runat="server">
<p>
Please enter the date of issue
</p>
<div class="fm-req">
<asp:textbox id="txtTVLicIssueDate" cssclass="tb size2" runat="server" onblur="return true;"></asp:textbox>
<cc2:calendarextender id="caleTVLicIssueDate" runat="server" targetcontrolid="txtTVLicIssueDate"
popupbuttonid="ibnTVLicIssueDate" popupposition="BottomLeft" animated="true"
format="dd/MM/yyyy">
</cc2:calendarextender>
<asp:imagebutton id="ibnTVLicIssueDate" runat="server" imageurl="../images/img-calendar-day.png"
alternatetext="Calendar" tooltip="Pick Date" cssclass="date-picker" />
<asp:requiredfieldvalidator id="rfvTVLicIssueDate" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
display="Dynamic" errormessage="Required" setfocusonerror="true" validationgroup="TVLic"></asp:requiredfieldvalidator>
<asp:comparevalidator id="cmvTVLicIssueDate" CssClass="error" runat="server" errormessage="Not a valid date"
controltovalidate="txtTVLicIssueDate" operator="DataTypeCheck" type="Date" setfocusonerror="true"
validationgroup="TVLic" display="Dynamic" cultureinvariantvalues="true"></asp:comparevalidator>
<asp:customvalidator id="cuvTVLicIssueDate12Months" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
validationgroup="TVLic" display="Dynamic" onservervalidate="cuvDocIssueDate12Months_ServerValidate"
errormessage="Document must be less than 12 months old."></asp:customvalidator>
</div>
</asp:panel>
<asp:panel id="pnlTVLicApprove" runat="server">
<asp:LinkButton id="lbnTVLicApprove" runat="server" CssClass="screen-hide"
alternatetext="Confirm TV Licence" tooltip="Confirm TV Licence" Text="OK" CausesValidation="false" OnClick="lbnApproveConfirm_Click" />
<asp:imagebutton id="ibnTVLicApprove" runat="server" imageurl="../images/img-accept-doc-off.png"
alternatetext="Approve" tooltip="Approve" cssclass="approval-btn" causesvalidation="true" validationgroup="TVLic" OnMouseDown="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" OnClientClick="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" />
<span class="approval-label">Accept document:</span></asp:panel>
</div>
</asp:panel>
The app is written in c# but i havn't posted any actual code as all the user code related to this markup seems to work fine.
The problem is the CheckBox chkTVLic has causes validation set to false and autopostback set to true. So whatever happens when i check and uncheck the checkbox it should postback. Most of the time this is exactly what it does and the result is to show and hide pnlTVLic when it is checked and unchecked. However if any on the validators within the panel fire, the checkbox does not cause a postback the first time. It will on all subsequent times but never the first. However it should ALWAYS cause a postback. What could be stopping it. Before someone asks there is no use written client side code, everything is pure .net markup and c# code.
I don't see why it shouldn't postback when you check/uncheck the checkbox, but if the only purpose of that checkbox is to hide/unhide a panel, I would rather do it in javascript. Doing a full postback to the server just for hiding some panel seems really bad.
In javascript you can do this to hide the panel:
document.getElementById('<%=pnlTVLic.ClientID%>').display='none';
And this to show it:
document.getElementById('<%=pnlTVLic.ClientID%>').display='block';
It's going to be so much faster and better. Just put a regular checkbox instead of the ASP.NET one and subscribe to the onclick event.
Sorry, one more comment:
I think you are wrong when you say that the checkbox should ALWAYS cause a postback. No, if one of the validators fires inside the panel, the checkbox will not cause a postback until the condition is satisfied.
This is what I did and it worked.
on checkbox onclick event I disabled all the validation controls and immediately did Page_ClientValidate(); and it worked.