I created an entry template for data which is repeated several times. Now I want to make sure at least one of these items is filled in.
the way I do this is use the Count property from a generic list List to see how many are are used(there's some logic in my control that allows me to just pick out the entries that are actually used).
Is there any way I can add something to the validationsummary of the page to notify the user that at least one itme needs to be filled in.
I want to do the count-check in the code behind ...
if (EnteredClasses.Count > 0)
{
//do stuff here
}
else
{
//show validation error
}
I would use the CustomValidator control, inline with the ServerValidate function and also check for the Page.IsValid. The error message then for your CustomValidator control will show up in the validation summary if the ServerValidate function sets the args.IsValid to false.
Andrew
Use a CustomValidator. When using these you can write any logic you like.
I would cheat and merge two methods. As per your question you seem to require the use of the validation summary so you need the basic asp validation. Then you need to use a custom validator.
The problem with the custom validator, is that you must attach it to a particular control and then implement the javascrip method
function ClientValidate(source, agruments)
{
// Do your check here where source is the span for the validator and
// argument is an object with .value the value of the control to which
// the validator is attached and .isvalid that indicate if the validation
// checks out (that's what you set to true or false)
}
The problem if that you want to validate multiple items not juste one. so I would implement this method and ignore arguments.value to then use jQuery to check your whole form and do whatever validation you need and then set isvalid.
Related
I have an ASP.NET web form with validators that are disabled. I want to enable validators only AFTER Submit button is clicked and based on if the control is visible or not.
Here is my function:
protected void Submit_Click(object sender, System.EventArgs e)
{
if (ddlSite.Visible){
rfvSite.Enabled = true;
base.Validate();
} else {
rfvSite.Enabled = false;
}
The above code is working fine. But now I want to check if page is valid or not. If the page is Valid then I want to process the form. I don't know how to do this.
You have multiple options:
Try to split your controls into multiple validation groups, which will only validate the controls in a specific group upon submit.
Write your own custom validator. A custom validator can declare a client validation function and a server validation function. Creating a custom validator is well-documented.
As StevieB mentioned, if you hide the controls server-side, the validators won't be fired. If the decision to hide them is made on the client, that's more difficult.
Hook into the client-side validation functions and manipulate the validators. This can be difficult and may require changing internal ASP.Net scripts. Sometimes it's the only way to make web form validation do what you need it to. See this question for an example of extending/modifying validator behavior.
Since I'm new to asp.Net c#, I was wondering is someone can give me an
example or idea as how to achieve this?
Consider using ASP.Net MVC instead of web forms. The web forms model is old and "fights" you on tasks like this. If you are just starting out, I'd suggest at least investigating MVC and see if your time is better spent.
A lot of jquery plugins was created for this reason :
http://tinyurl.com/qetudk8
I believe if you add a
if(Page.IsValid) {
// Code here
}
around the button that is submitting the form, the page should fire off validation errors.
I am designing a webapp using ASP.NET and jQuery and I could use some advice.
Currently, the ASP.NET page renders an unknown number of elements that perform an action when clicked. Javascript on the front-end handles the click event based on which specific element was selected.
Each element is embedded with information that the javascript function requires. This informaion is added as extra attributes. So for example, a given element might look like
Link 123
jQuery then attaches a click event and uses the extrainformation attribute as a parameter for an internal function. Each element has 3-5 parameters.
This works great, but I have heard recently that it might not be best practice since it is not WC3 compliant. One possible solution would be for each element to directly call the internal javascript function with the necessary parameters. However this makes me uncomfortable because I lose the separation of concerns between rendering the page and executing the client-side logic.
Is there a better way to do this? Or maybe I'm just overthinking it?
Yes there is a better way, its called HTML5 Data Attributes you can access them from jQuery using the $.data() interface.
For Example:
//instead of
Link 123
//use
Link 123
//then access it like
var info = $('#123').data('info');
alert(info); //alerts: 'something important'
Basically anything starting with data- is stored as data about that element in the DOM, jQuery can access this data via the $.data() function.
In my case I would like to dynamically add validators to my control based on given logic. For each control I first check something in my DB and if it goes aout that field is required I would like to add requiredField to that control. I firt iterate through each control and if its required I add attribute required="true".
I added this code but it doens work I mean nothing happens, none validation is being made.
if(gc.Attributes["controlid"] != null)
{
RequiredFieldValidator validator = new RequiredFieldValidator();
validator.ControlToValidate = gc.Attributes["controlid"];
validator.ErrorMessage = gc.Attributes["errormessage"];
this.Controls.Add(validator);
}
Thanks for any suggestions.
You also have to add it to the Page's validators collection in order for the server side validation to occur. Adding it just to the page controls collection as you did is what is needed to get the JavaScript validation to get rendered to the browser.
Page.Validators.Add(validator);
Are you adding your Validator control to the same container as the control it validates? Validator controls require the target control to be in the same INamingContainer.
How Can We Check The Validation Of RadCaptcha From Code Behind - With Custom Validator(ServerValidate) / WithOut Using And Setting ValidationGroup ?
thanks In Future Advance
Best Regards...
You can check whether the correct code was entered by using the RadCaptcha's IsValid property. To be sure that validation has occurred please invoke the RadCaptcha's and Page's Validate method:
RadCaptcha1.Validate();
Page.Validate();
if(RadCaptcha1.IsValid)
{
//TODO: Add your logic here
}
My guess is that you will have to find a way to associate your custom validator with the textbox rendered by the captcha. You should be able to access it via the ValidatedTextBox property of RadCaptcha.
I have 10 defined textboxes with strings.
I have to check all if they are not empty while clicking ok button
whats the cleanest way to check them all and when function is at end. each checkbox which was empty to give this a specific CSSclass. perhaps. ClassError. ( which highlights red)
I'm happy for answers.
I would add RequiredFieldValidators to them, as well as a ValidationSummary control.
edit: You can also add fancy AJAX effects with the ValidatorCallout from the AJAX toolkit.
edit: Validator controls also support client-side validation.
Using javascript or C#?
Javascript I will create an array of textbox and loop through it.
C# just go FindControl within a Panel or the Container of the text box and go something like this
foreach(Control C in ContainerID.Controls)
{
if ( C is TextBox )
{
if ( String.IsNullOrEmpty((C as TextBox).Text))
{
// Do things this way
}
}
}
Something like this would work on the client side using jquery:
$('input').filter(function(){return this.value=="";}).css("CSSclass");
edit: I just saw the C# tag, I'll leave this here for posterity though.
What you're doing might be simple enough to do easily with some custom javascript code, though I would say that in general the built in validator controls are both easier to use and more robust than a simple validation routine that someone might write. In addition to client-side validation, the validator controls also perform server-side validation to ensure that the data submitted is truly valid, in case someone has javascript disabled in their browser.
If validator controls are included on the page, then they will include some javascript functions that you can invoke, such as Page_ClientValidate(). I believe this will return a boolean telling you whether validation passed and will trigger the visual indicators that identify what the errors are. You can execute Page_ClientValidate('') to trigger only a group of validator controls; actually I think you must do that in order to trigger validation on any validator controls that have a value in their ValidationGroup property, as I don't think Page_ClientValidate() will trigger their validation logic.
There is a CustomValidator control that you can point to your own client-side function if you want, in case you do have some special validation logic that you can only implement through a custom javascript function. This is nice to use, because then your custom javascript function will be executed by the built-in validator framework along with any other built-in validator controls that you might choose to include on the page.
Side note regarding client-side validation: I suggest that you avoid doing the following to trigger validation:
onclick="return myValidationFunction();"
because if there is any other javascript code being injected into the onclick event, your return statement will prevent it from executing. So instead, I suggest doing this:
onclick="if(!myValidationFunction()) return false;"
That's bitten me enough times that I thought I'd just throw that out there. This problem is particularly noticeable if you have an ASP.Net button on which you've set the UseSubmitBehavior property to false, as it will cause the button to render as an HTML "button" control instead of a "submit" control, and as a result, executing a return statement, either true or false, will prevent the button from triggering a postback.