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.
Related
Currently on my website when a user logs in if their role in their row within the Users table is equal to 0 it will give the user 3 options on options.aspx, however if not it will take them to home.aspx
Now I am trying to just do this on one whole page, so if the user was to be directed to the options page, instead it would take them to the home page where the user would have to click an option amongst the 3, if the user has already selected an option then it would load a different content placeholder.
Is a content placeholder what I need to achieve my idea? If so, wouldn't it be abit too easy to get out of the situation since the client could inspect element and edit the visibility on their side?
When you set the Visible="false" property on a server-side control (runat="server") the tag is NOT rendered to the browser. As a result, the client cannot set it as visible. So for example:
<asp:PlaceHolder runat="server" Visible="false">SomeText</asp:PlaceHolder>
The SomeText will be nowhere to be found in the HTML if the user does View Source. If, however, you do a CSS style on a control such as style="display:none" then your concern is 100% valid as the user could just show this.
This same logic hold true for any ASP.NET server-side control. If you set Visible="false" it's never sent to the browser.
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
ASP.NET WebForms 4.51
Having read things like How to validate two groups of controls with 1 button and a validation group?
I have a form with the following logical inputs:
Name (TextBox)
Last Name (TextBox)
Profession (Drop Down List)
Profession has the following items:
Bus Driver
Taxi Driver
Not Applicable
Now based on the profession I display a number of other text fields. I have required field validators on all the fields 1..3 as well as the fields for the profession (4..8).
I have a single submit button which triggers the validation. So my question is what is the easiest way to validate controls: 1..3 should the selection be Not Applicable for profession and 1..8 for the other professions from a single button.
I can think of a couple of ways but they are all "messy" and I was looking for a better approach:
1) Have two required field validators per field - one when validating for Not Applicable and one for when a profession is selected. Toggle the validation group when the profession is selected for the button.
I tried this but it does not seem to work as I still get both validators firing per field.
2) Write custom client side validators that take into account what is selected in the drop down for profession.
A maintenance nightmare. I would prefer to use any built in functionality MS provides.
So has anyone got any suggestions on how to validate the same sets of controls differently based on what has been selected in another control?
You could write a simple javascript validation script. You can not validate the javascript on serverside using "page.Isvalid".
Your only option is to write a custom validator. I just don't know why that should be a maintenance nightmare. If you're using the javascript validation you should validate the values in the code behind anyway. Otherwise the user could just disable javascript and enter anything he wants.
Hope it helps...
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.
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.