Is it possible to change the CSS class of the validated control if it's not valid without codebehind or just with less code ex. Set automatic CSS class to "invalid" of all textboxes which have a required field validator?...
I don't like to make a check for each validator (isvalid) I have, manually.
Thank you and best regards
What you need is client side validation.
JQuery is perfect for that. you can use this nice tutorial:
http://www.webreference.com/programming/javascript/jquery/form_validation/
or use a plugin:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
one way or another - no need to run to the server and back, and you have full control of the validation process and its outcomes (so you can change the css accordingly)
Enjoy!
Related
Is it necessary to write and place all controls inside
#using (Html.BeginForm())
{
// HTML Elements and HTML Helpers.
}
while using [Required] DataAnnotations ?
I am facing strange issue in MVC5 based application. The problem is that I have used one property named e.g "Credit" in model and the datatype of this property is integer and set[Required] DataAnnotations above that property.
But I haven't used Begin form. So in this case validation doesn't fire. whereas If I write BeginForm then validation works.
So, Is it necessary to place all html elements & html helpers inside BeginForm to validate controls ?
Thanks
-Nimesh.
If you want the client-side validation to work, then yes the form controls etc. need to be within a <form> tag (as generated by the HTML.BeginForm helper). Server-side validation would still work regardless of this.
Like the commenter above, I would question why you want to have controls outside a form tag in the first place. Even if you plan to submit the data back using Ajax, it's better semantic design to use a form tag, because it's clear which data items belong together, and it also makes it much easier to gather the data to submit via ajax (e.g. if you have jQuery, you can use $("#myForm").serialize() to automatically collect the values from all the controls within a form and pass that to the ajax request).
We need to validate something when we post some data to the server, right. And for posting some data to the server you will need form tag, whether you use BeginForm() or the <form> tag. You need tags inside the form those will be validated by the server.
I guess, this will give the answer to your question. Enjoy!
I recently started working with some legacy ASP.NET stuff, and I've run into an interesting problem:
I've got a table displaying a few values, some of which are evaluated in C# server-side. They all work correctly. Then all of a sudden...
<td><asp:Label ID="Label2" runat="server" class='<%=SevenDayThresholdTooHighOrLow%>'><%=ChgFromSevenDaysAgoInfo%></asp:Label></td>
ChgFromSevenDaysAgoInfo is evaluated properly.
SevenDayThresholdTooHighOrLow is rendered as a string inside of the class quotations. That is
class="<%=SevenDayThresholdTooHighOrLow%>".
In the code-behind file, the two variables are declared in the same scope, and assigned values pretty much one after the other. My Visual Studio doesn't complain about not finding certain variables in code like it would if the property did not exist.
What other factors could be influencing this oddity? What could I have missed?
Thank you very much for your help, everyone!
Eli
EDIT: I took a look at what is the use of Eval() in asp.net and tried to set my tag up that way (class='<%# Eval("SevenDayThresholdTooHighOrLow")%>'), unfortunately to no effect.
That class attribute probably isn't being evaluated for server-side code, likely because class isn't a property of Label. (Label isn't an HTML element, it's a server-side control. So instead of HTML attributes it uses object properties.)
There's a CssClass property, you might try that instead. But even then the approach is different because the system may still not attempt to evaluate server-side code injected into already server-side components. Still, worth a try.
What should definitely work is setting the CssClass property in the code-behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
If you want to keep this out of code-behind (and who could blame you?) then another approach could be to replace the server-side control with an HTML element entirely. The idea being that if properties on this control aren't otherwise being set in server-side code, then does it really need to be a server-side control? If I remember correctly, a Label emits as a span:
<span class="<%=SevenDayThresholdTooHighOrLow%>"><%=ChgFromSevenDaysAgoInfo%></span>
You can't do that in just the markup code. You can't use server tags in a server control. You could use a data binding tag, but then you would need to trigger the data binding from the code behind anyway.
Just set the attribute from the code behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
I have been trying to apply the default ASP.Net Required Field Validator to an AsyncFileUpload control (from the AJAX Control Toolkit).
The Scenario
I have created a Web User Control. Let’s call it wucFileUpload.
wucFileUpload has an AJAX Update Panel that is wrapping the Required Field Validator and the AsyncFileUpload. (I have to use the AJAX Update Panel for some particular reasons).
wucFileUpload is going to be used in many pages, and in some of them, this control is going to be generated automatically, so I do not know how many of them I will have per page.
wucFileUpload has a property called Required. If Required is true, it will enable the Required Field Validator, to check if the AsyncFileUpload has been filled at least once.
My try
I found this solution here on StackOverflow, and I tried to apply it. However, my case is a little bit different, from the one represented on that question.
My thoughts
I really liked the idea of having a hidden Textbox or a HiddenField. Because a single AsyncFileUpload can upload N number of files.
The Textbox could get some value on the first time OnClientUploadComplete runs… this way I’ll know the user has uploaded at least one image.
But I would also need a way of clearing that Textbox, because the user can delete the image that he just uploaded. And if the does that, then the field must be validated again.
How can I prevent the user from entering anything but alpha characters in my textbox?
Update
Forgot to mention that its a dynmic control (created on the form when the user clicks a button).
With built-in ASP.NET features only, you could use the <asp:RegularExpressionValidator> control, with a regular expresion like [A-Za-z]*. This will validate on server side (which should be your main concern), and give a user-friendly error message on postback. There are ways to use these controls for clientside validation as well if you're using the MVC framework, but I don't know how or how well that works in WebForms.
Using jQuery, there are endless possibilities for clientside validation as well with the jquery.validation plugin.
The AjaxControlToolkit has a FilteredTextBox extender that allows you to specify a combination of both lower- and uppercase letters.
I would set a CssClass on the dynamically created textbox, then use jQuery and some custom javascript/regex to filter any input field with that CssClass on the keyup event.
The filtered TextBox extender is good, and you could probably use that as well, for me, the jQuery solution would be a little easier and flexible.
I have an Asp.Net repeater, which contains a textbox and a checkbox. I need to add client-side validation that verifies that when the checkbox is checked, the textbox can only accept a value of zero or blank.
I would like to use one or more of Asp.Net's validator controls to accomplish this, to provide a consistent display for client side errors (server-side errors are handled by another subsystem).
The Asp:CompareValidator doesn't seem to be flexible enough to perform this kind of complex comparison, so I'm left looking at the Asp:CustomValidator.
The problem I'm running into is that there doesn't seem to be any way to pass custom information into the validation function. This is an issue because the ClientIds of the checkbox and the textbox are unknown to me at runtime (as they're part of a Repeater).
So... My options seem to be:
Pass the textbox and checkbox to the CustomValidator somehow (doesn't seem to be possible).
Find the TextBox through JavaScript based on the arguments passed in by the CustomValidator. Is this even possible, what with the ClientId being ambiguous?
Forget validation entirely, and emit custom JavaScript (allowing me to pass both ClientIds to a custom function).
Any ideas on what might be a better way of implementing this?
I think the best way would be to inherit BaseValidator in a new class, and pass those IDs to your control as attributes. You should be able to resolve the IDs within your validator, without knowing the full client side ID that is generated at runtime. You should get the data validating on the server first, and on the client second.
Can you not put the CustomValidator inside the repeater? If not, you can create it dynamically when the repeater is bound and user FindControl()
protected MyDataBound(object sender, RepeaterItemEventArgs e) {
(CheckBox)cb = (CheckBox)e.Item.FindControl("myCheckboxName");
(TextBox)tb = (TextBox)e.Item.FindControl("myTextBox");
}
...or something like that. I did the code off the top of my head.