CompareValidator with aspCalendar - c#

I ve two aspCalendar controls on the website. I would like to perform validation (date from Calendar2 should be greater then date from Calendar 1).
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="CompareValidator"
ControlToValidate="Calendar2"
Operator="GreaterThanEqual"
Type="Date"
ControlToCompare="Calendar1"
/>
There are a lot examples of validation but with ControlToValidate as textBox or label, What about more complex Controls, is there any way I can tell which property from that control has to be used?
Maybe Client side validation ? But how to do this ?
Thanks for help

I would recommend using a customvalidator, which gives you complete control over the way the validation occurs. The control to validate may not accept the calendar; however, you can point it to a textbox, but behind the scenes in the servervalidate function validate your calendars, and so that is a workaround to making the validators work with controls like the calendar.
You could use client validation, but it depends on what you are trying to validate and how easy it would be; there is a lot of markup with the calendar control, so that might be hard...
HTH.

Related

How to remove validation of other fields if one field is validated in Asp.net Validation control?

I have three upload buttons in my asp.net form and I am using asp.net validation control. I want user to upload anyone of those files. I can do it with jquery but please tell me how to achieve it by using Asp.Net validation controls.
Thank you.
You need to use validation groups, see https://msdn.microsoft.com/en-us/library/ms227424(v=vs.140).aspx for specifics.
Essentially you'll have your upload button:
<asp:FileUpload runat="server" ID="fu1" ... />
Then your required field validator:
<asp:RequiredFieldValidator ID="vldFu1" runat="server"
ControlToValidate="fu1" ValidationGroup="fuGroup1" ... />
And your button:
<asp:Button runat="server" ID="cmdFu1" ValidationGroup="fuGroup1" ... />
That way, your controls will use separate validations limited to each group, and they'll not interfere with each other.
As you say, you can use JavaScript to enable and disable validators - See here for details on this:Enable/Disable asp:validators using jquery
EDIT TO ADD
If you need to have at least one of the three completed, you'll need a customValidator, there's a number of answer already on SO with the details:
asp.net required field validator for at least one textbox contains text
Using validators to ensure that user filled either one of two required fields
asp.net validate textbox - at least one text box must have data in
If you would like specific help, please post code examples and specific errors / problems

Selectively applying validation on textboxes with C# asp.net

I am working with C#, asp.net 3.5. I have a dropdown list which has values like:
Pick last 24 hrs data
Pick last 48 hrs data
Pick data between dates
When I select Pick data between dates it shows two textboxes where user can set "From Date" and "To Date"
I want to apply a validation that if user selects between dates option, dates' textboxes must be filled, for other dropdown values textboxes' values don't need to be checked.
Should I do it through jquery/javascript (which I am already doing) Or is there a way to achieve this by using existing validation controls ?
Yup , You can do it by compare validation
See this sample :
<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate"
ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"
ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>
You may add custom validation methods in js.
Also you may add custom attribute on elements in your model (in C# code) and write your custom behavior (for example read this answer).
In my opinion if you even do this validation using javascript you should anyway duplicate same validation on server. In other case it is possible to disable javascript in browser and send not valid form to server. You should create javascript validation if you want to provide nice looking UX.
If you are looking for pure asp.net approach you can do as follows:
1) Set dropdown Autopostback="true" which will trigger postback each time you change value in drop down.
<asp:DropDownList id="ddl"
AutoPostBack="True"
runat="server">
2) Add validation controls for your date time fields.
<asp:RequiredFieldValidator />
3) Disable or enable validation controls during postback depending on selected drop down value.

Custom validatior in ASP.net

I'm working on an ASP.NET/C# app.
I have 2 text boxes and I need to validate if the input text for both of them are not both null
ex:
if(string.IsNullOrEmpty(TextBox1.Text) && string.IsNullOrEmpty(TextBox2.Text) )
//FAIL!!!
else
// OK!
that is, at least one txtBox has values
I was planning on using a custom validator but it seems that the validation function only gets called when something is written on the textBox i'm using has the 'control to validate'. Now, that doesn't work for me since I want to show an error message when both text boxes are empty. Is there a way to make the validation function to be called with, for example a postback? Or is there any other better approach to this case than the custom validator?
tks
If you set the 'ControlToValidate', then the validator will not fire if that control is empty. However, for a CustomValidator you can leave that empty so it will always fire.
Because the built-in validators only analyze the state of their own controls, you are forced to go with a CustomValidator for this functionality. I don't know of any better approach that still uses ASP.NET server-side validators. As Hans said, be sure to leave ControlToValidate empty, and then do the validation as in your code sample.

How to use the Validator controls with controls other than TextBox?

I want to use the Validator control to validate a FCKEditor rich text control. Is there a way to do this on either client and/or server side?
And a broader question, is there a way to use the Validator controls for anything other than text boxes?
You can use CustomValidator control to perform custom validation logic. This control actually doesn't even require ControlToValidate to be set.
It is possible to use a
CustomValidator control without
setting the ControlToValidate
property. This is commonly done when
you are validating multiple input
controls or validating input controls
that cannot be used with validation
controls, such as the CheckBox
control.

rendering multiple Calendars bloats html, bogs down page

note: I am using the Infragistics control because this is inherited legacy code, but I am not above rewritting with an ASP.NET control if that is a better solution.
I have a Repeater control that uses an Infragistics WebDateChooser to select a date for a record. Let's say each item in the Repeater represents a customer, and I am selecting an activation date or something of that nature. It could be any time past, present, or future.
When you render this control 20 times, it writes all of the heavy html for showing all of the dates (month names, weekdays, etc etc) 20 times and bloats the html dramatically. This causes the browser to really struggle with rendering the page in any reasonable amount of time, even for 20 records (paging implemented on the repeater just to prevent the browser from crashing on massive page). This is also true to a lesser (but still significant) degree with the standard ASP.NET calendar control.
What I'm looking for is a way to possibly make all 20 date choosers share the resources of 1 calendar so they don't each need to render their own strings and crap for displaying verbose dates.
EDIT:
I understand many users have not used Infragistics, but it's still just as true with the standard, built in ASP:Calendar control. Put one in a repeater and display n > 20 times. It bogs down the browser when rendering.
Also, just to clarify incase this matters to anyone's potential solution, this codebase is on .NET 2.0 and has to support IE6.
Another thing you might consider is to have one instance of the calendar on the page. When the user clicks a textbox that "activates" the calendar, you can use a client-side javascript framework like jquery to show the calendar and move it the correct expected position. Once the date is selected, store the selected date in the correct text box and hide the calendar again. You'll have to write some javascript but it beats downloading all the extra bloat!
If what you are looking for is a Datepiker that is called and displayed on each date field in a grid, calling a JavaScript calendar is the most efficient. Check out the JQuery ui calendar and just put the call on each field -- see: jqueryui.com/demos/.
HTML -- note the class is the same and ID different:
<input type="text" class="datepicker" id="d1" />
<input type="text" class="datepicker" id="d2" />
JQuery then selects the css class:
$(.datepicker.each(function() {
$(this).datepicker();
});
The older ASP.Net solution is for a control to be declared and dynamically instantiated on the server when the click even is fired. There are many example of this on lots of blogs. Or 20 of them can be created on page load and placed in a datagrid or something. But what if the datagrid has 100 entries? It cannot scale.
But there is a calendar in the AJAX control toolkit that is created once on a panel and then that panel is displayed where you tell it. It is one calendar, shown many times.
<asp:Panel ID="panelCal" runat="server">      
<asp:UpdatePanel ID="update" runat="server">           
<ContentTemplate>   
<asp:Calendar ID="theonlyCal"runat="server"/>           
</ContentTemplate>      
</asp:UpdatePanel>
</asp:Panel>
Now say there are 20 rows:
<asp:TextBox ID="twenty" runat="server" />
Now each text box needs a popup control extender.
<ajaxToolkit:PopupControlExtenderID="twentExtenders"runat="server"
TargetControlID="twenty"
PopupControlID="panelCal"
Position="Bottom" />
ASP.NET AJAX can use lots of bandwidth.
I haven't used the control you are so I can't say if this one will work for you but I highly recommend: http://www.dynarch.com/projects/calendar/ It's a javascript calendar so the user will only download the js includes once. After that the creation calls are just a few lines.
It's not immediately "drag-and-drop" compatible with ASP.Net. I recommend wrapping it in a user control.
While it doesn't unify into one instance of the control, could you use the CalendarExtender that comes with the AjaxControlToolkit. I just built a small example on my machine and it didn't bog the page down that much.
Well the example at Infragistics definitly shows that the control was built to be reused like this. They aren't using a Repeater of course but it all works the same in the end. I would look into how the UltraGridColumn is working with the WebDateChooser to display the dropdown.
Of course, this won't help with the more general problem. For that I would do as the others say and create one control and use javascript to display it where it is needed, when it is needed.
If you really don't want to write any javascript yourself you can take advantage of the toolkits. You could create an instance of whatever calendar (not the dropdown version) and use the AjaxControlToolkit's modalpopup, Infragistic's WebDialogWindow or something similar (perhaps with less dialogy more floaty) to display it.
In the end all the options have one thing in common. They create a single calendar outside of the repeater and display that instance of the calendar on demand.

Categories

Resources