We recently upgraded our application from .Net 2/vs 2005 to .net 4.5/vs2012 and are seeing exactly same issue noted in ASP.NET Validator Issue when setting Display ="Dynamic" this question. My submit button moves down on validation failure and we have to double click to trigger postback. Adding <br /> before submit button is not acceptable solution for us as we have over 200 pages. Is there any generic solution to this problem.
below is the code snippet:
<asp:textbox id="txtCancellationDays" runat="server" maxlength="3"/>
<asp:rangevalidator id="rvCancelDays" runat="server" controltovalidate="txtCancellationDays"
maximumvalue="999" minimumvalue="0" type="Integer" text="Cancellation cutoff days must be a number between 0 and 999." />
<asp:requiredfieldvalidator id="rfvCancelDays" runat="server" display="Dynamic" controltovalidate="txtCancellationDays"
text="Cancellation Cutoff Days is required" />
<asp:comparevalidator id="cvCancelDays" text="Cancel Days must be less than or equal to Waitlist Days"
runat="server" controltovalidate="txtCancellationDays" controltocompare="txtWaitlistDays" type="Integer"
operator="LessThanEqual" />
Related
I'm having a situation where i'm having a Calendar control in my page and when user selects a date it should check whether date selected is greater than today's date.
I tried a lot and Googled it as well, but in every place they are taking value using a textbox and i want the same to be done with a Calendar control.
Contrary to popular belief, you actually don't need any C# code to get this done! All you need is some extenders and validators on your date textbox. So let's say I had two textboxes -- one named txtStartDt and one named txtEndDate; this is how the extensions for them would be placed:
// Start Date
<asp:TextBox ID="txtStartDt" runat="server" CssClass="textboxint" /> // the main control itself
<ajax:CalendarExtender ID="calStartDt" runat="server" PopupPosition="BottomRight"
TargetControlID="txtStartDt" /> // allows the control to behave like a calendar picker
<asp:CompareValidator ID="CVStartDt" runat="server" Type="Date" Operator="LessThan"
ErrorMessage="Start Date must come before the End Date" ValidationGroup="ReportDate"
ControlToValidate="txtStartDt" ControlToCompare="txtEndDt" Display="None" /> // THIS is the main solution to your problem
<ajax:ValidatorCalloutExtender ID="VCEStartDt" runat="server" TargetControlID="CVStartDt"
HighlightCssClass="valCalloutint" /> // Checks that the CV is working correctly
<asp:RequiredFieldValidator ID="RFVStartDt" runat="server" ControlToValidate="txtStartDt"
Display="None" ErrorMessage="Start Date is required" ValidationGroup="ReportDate" /> // ensures a value was added to the textbox
<ajax:ValidatorCalloutExtender ID="VCEreqStartDt" runat="server" TargetControlID="RFVStartDt"
HighlightCssClass="valCalloutint" PopupPosition="BottomRight" /> // Checks that the RFV is working correctly
// End Date
<asp:TextBox ID="txtEndDt" runat="server" CssClass="textboxint" />
<ajax:CalendarExtender ID="calEndDt" runat="server" PopupPosition="BottomRight" TargetControlID="txtEndDt" />
<asp:CompareValidator ID="CVEndDt" runat="server" Type="Date" Operator="GreaterThan"
ErrorMessage="End Date must come after the Start Date" ValidationGroup="ReportDate"
ControlToValidate="txtEndDt" ControlToCompare="txtStartDt" Display="None" />
<ajax:ValidatorCalloutExtender ID="VCEEndDt" runat="server" TargetControlID="CVEndDt"
HighlightCssClass="valCalloutint" />
<asp:RequiredFieldValidator ID="RFVEndDt" runat="server" ControlToValidate="txtEndDt"
Display="None" ErrorMessage="End Date is required" ValidationGroup="ReportDate" />
<ajax:ValidatorCalloutExtender ID="VCEreqEndDt" runat="server" TargetControlID="RFVEndDt"
HighlightCssClass="valCalloutint" PopupPosition="BottomRight" />
Note that the extra ValidatorCalloutExtender tools are not redundant, but actually allows the program to prevent the user from entering an empty value for one of the textboxes.
Hope this helps!!
The calendar control should have a property called SelectedDate. You can compare this property with any other DateTime property. The code snippet below will check if the SelectedDate is greater than today.
if (Calendar.SelectedDate > DateTime.Now())
{
}
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 3 filter textboxes on my asp.net page. Each textbox is assigned 2 regularexpressionvalidators to them, 1 for numeric and 1 for alpha. It detects the validations correctly but when i press my search button, it still postback even when the validation tooltip appears(showing the filter is invalid). Any ideas why its "ignoring" the validation and posting back anyways? Here is my definition of the 3 controls and their validations.
<td>
<asp:TextBox ID="txtAcctFilter" runat="server" BackColor="#FF6600" TabIndex="1">Enter Account No</asp:TextBox>
<asp:RegularExpressionValidator ID="revAcctFilter" runat="server" Text="*" ControlToValidate="txtAcctFilter"
ToolTip="Enter Valid Account" ValidationExpression="^\d*$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="revAcctFilter2" runat="server" ControlToValidate="txtAcctFilter"
ValidationExpression="^Enter Account No$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="txtYearFilter" runat="server" BackColor="#FF6600" TabIndex="2"> Enter Year</asp:TextBox>
<asp:RegularExpressionValidator ID="RevYearFilter" runat="server" Text="*" ControlToValidate="txtYearFilter"
ToolTip="Enter Valid Year" ValidationExpression="^20\d{2}$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RevYearFilter2" runat="server" ControlToValidate="txtYearFilter"
ValidationExpression="^Enter Year$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="txtMonthFilter" runat="server" BackColor="#FF6600" TabIndex="3">Enter Month</asp:TextBox>
<asp:RegularExpressionValidator ID="RevMonthFilter" runat="server" Text="*" ControlToValidate="txtMonthFilter"
ToolTip="Enter Valid Month" ValidationExpression="^(1[0-2]|0?[1-9])$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RevMonthFilter2" runat="server" ControlToValidate="txtMonthFilter"
ValidationExpression="^Enter Month$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
This can happen if CausesValidation is not set on your search button. You might check that.
Incidentally, if you have two contradictory regular expressions that you want the content of each textbox to meet, you might not ever be able to submit, because both can't be satisfied at the same time (except by an empty string).
ValidationExpression="^Enter Account No$|^\d+$" is the correct answer for this. I had to use the ^ $ tags for each side of the | to make it validate both sides. This allowed me to go back to just one expression. I also added the causesvalidation for each control to prevent a postback. So far its working :)
i am beginner for asp.net . I now facing a problem . How can i force a user to select option from ajax control toolkit autocomplete?
Here is my sample code
<cus:cusTextBox ID="txtCMemberID" runat="server" Action="Edit"
CssClass="inputTextM" OnTextChanged="txtCMemberID_TextChanged"
AutoPostBack="True"></cus:cusTextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"
ServicePath="~/Module/Common/autoComplete/acLewreMember.asmx"
MinimumPrefixLength="1" ServiceMethod="GetSuggestedStrings"
TargetControlID="txtCMemberID" CompletionInterval="10"
CompletionSetCount="3" EnableCaching="true"
CompletionListCssClass="completionListElement"
CompletionListItemCssClass="listItem"
CompletionListHighlightedItemCssClass="highlightedListItem"
FirstRowSelected="True"
ShowOnlyCurrentWordInCompletionListItem="True">
</asp:AutoCompleteExtender>
As you see the textbox is a custom control. Is that any way to force user to select option from autocomplete?Please provide me a sample code also. thanks
ASP.NET includes a useful feature called a validator.
<asp:TextBox ID="TextBox2" ValidationGroup="Group2" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ErrorMessage="*Required" ValidationGroup="Group2"
ControlToValidate="TextBox2" runat="server" />
<asp:Button ID="Button2" Text="Validate Group2"
ValidationGroup="Group2" runat="server" />
Here is the resource link for msdn: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx
I'm not sure how these work with custom controls as I'm relatively new at asp. They also have a custom validator which might be more suited for your purposes, but like I said. I don't know exactly.
Note: If the client is using an old browser this won't work, so you also want to check on the server that this field isn't empty by using:
if (Page.IsValid)
{
//stuff to do in your event handler
}
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.