Validate data when textbox loses focus - c#

In my ASP.Net web application, there is a textbox to receive ProductId. When the user enters data into the ProductId textbox and moves to the next textbox, I want to validate the data against a table and if matching record is found, I want to display Product details in different controls (readonly and normal textboxes) else I want to display a message about "New Product" and continue. I do not want to do the verification AFTER the user enters all data and press the Save button, but it should be done immediately. I was hoping that the Web Form textbox would have a LostFocus event, but do not know how to handle the requirement.

ASP.NET Validators don't have an ajax style immediate check.
Attach a custom validator to your textbox that hooks up to a validation method server side.
Using javascript, you'll need to hook up an eventhandler to the focusout event and make an ajax call. That call will send the value back to the server and get a response of valid or not.
This site : http://brian.dobberteen.com/code/jquery_ajax_custom_validator/ will help you with a lot of it, but one thing is this will fire only when the page is submitted. You'll want to hook up an additional event handler to your textbox to call the validation method on focusout

It's easy solution, read this article:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.servervalidate.aspx
You just need to preform javascript call to trigger server validation, use event onlostfocus.

Related

Dropdown list onchange function - update value

I have a Y/N dropdown list that I would like to update from the view (instead of updating it on the Edit page). For example, if I select "Yes" I will be redirected to the index page which will display "Yes". If I change it to "No" it will show "No". The result will be saved to the database. Basically, I want to call the edit method for the Y/N property on my index page.
What is the best way to go about this?
Here is one way to do it :
Wrap the dropdown list in a form that will POST to the server.
Not necessarily the Edit action... it could be some more specific UpdateYesNo action.
Listen for change events on the dropdown list.
Submit the form programmatically from within the change event.
Handle the request and redirect the client to the appropriate page.
Since this is a full POST request and not an $.ajax call we let AspNet Mvc handle the redirection.

Update Database on textchange

In an ASP.NET webform, I have 3 TextBoxes, each linked to a different SQL Database Table. Now I want to store data into table when Textbox Change event occurs. I can do successfully with make Update Query in TextBoxChanged event. Now What I want is when this update operation going on I dont want it to make interrupt my page and other operations I want to perform. I use an Asynchronous Postback from the TextBoxChanged event.
The problem that I am having with this is:
When I change one TextBox and click on another TextBox, and hit "backspace" inside that textbox, the page reacts as if it was a browser back button-click, and redirect to other page.
I hope you can understand the question I want to perform TextBoxChanged and handle the asynchronous postback without interrupting other operations.
You will have to use jQuery for the capturing of the textbox changing. However, you cannot connect directly to onchange as the changed value is not stored until after the event. You could probably tie to the blur event if you only want to capture when the user changes and leaves the textbox, though. Otherwise, you will have to use a timeout to continuously check if the value has changed ( How to detect a textbox's content has changed )
Within that event, you should use AJAX to make the save calls for persisting the value.

Is there a way to NOT load a specific control in ASP.NET/C#?

I've a container (RadDockZone - Telerik), and inside it, a textbox field with an "Asp:RequiredFieldValidator", which makes it mandatory. Every time that I work with AJAX operations, the entire container is "reloaded", including the RequiredFieldValidator. This makes the ValidationSummary show the same error message twice, until the page is completely reloaded (I'm using RadAjaxManager to solve the AJAX operations).
So, how can I "don't reload" a specific control (in this case, the RequiredFieldValidator) using C#?
Regards!
If you place the following in page load if(page.Isvalid) the required field validator will be fired once after submitting that form. The message will not be displayed twice.
If you do not include the textbox (or its wrapping containers) in the list of controls updated by the ajax manager, the textbox and the respective required validator should not be updated on ajax requests and the error message should not be visualized.
You should use validation groups. This lets you group all of your validation controls into a group. Then put this same validation group on your submit button and those validation controls will only validate when that particular button is clicked.
Property is assigned on the validation control and button using the ValidationGroup property.
Thomasvdb's suggestion of setting CausesValidation to false also might be a good way to do it.

Validating textbox content on blur by calling server-side method without affecting page behaviour

I have a textbox in one grid-view column where upon entering a particular value and losing focus of the textbox, should post to the server to get the text validated through a server-side method. If the entry is valid, a result set to fill rest of row cells would be returned, else the bgcolor of the textbox needs to be changed to Red.
I tried posting back through the obvious way, i.e. making the textbox's autopostback as true and wiring up a server-side OnTextChanged event handler to validate the entered value.
It is working with this setup, but is also affecting the remaining page controls behaviour. For example, if I click a button in some other grid after entering some text in the textbox, the OnTextChanged handler gets called thus preventing the button's click event, which I also wish to call to execute its functionality.
Kindly suggest what alternatives/corrections I should carry out to enable textbox content server-side validation plus making the other controls/updatepanels work as expected.
Me dumb. I tried everything from creating PageMethods, UpdatePanels to jQuery as hinted in lincolnk's reply. But the thing which finally worked was removing the Autopostback attribute from the textbox control.
After removing it the OnTextChanged event executed each time any server postback was initiated after changing the text. Thereby, executing both the OnTextChanged method and the other control's method. :)
I can think of a couple general approaches.
Create a web service with your validation routine and manually make the call (jQuery or whatever) when the text changes. Manually update the client display when you get a result.
Convert your gridview column to a templated field. Add a CustomValidator and wrap the textbox and validator in an UpdatePanel. Set the textbox to auto-postback and the UpdatePanel to conditional update so only the one you are using is refreshed.
Option 1 is kind of an end-around the typical asp.net process, and you would still want to validate everything on the server-side when the page is posted back.
Option 2 might have performance issues, since you're hitting the page again every time you do a validation.

Client side event handler on validation for asp.net validator controls

I have an asp.net form with bunch of sections that can be expanded/collapsed and are normally collapsed. Now, most controls on the form have RequiredFieldValidator or some other validators attached. If the user tries to submit the form with required fields not filled out, for m is not submitted but because most sections are normally collapsed, the user doesn't see the validator text (like exclamation mark on the right of a text box) prompting to fix the error.
What I want to do is for the controls that fail validation, to expand their parent containers so user could see those failed controls, but for that I need to hook up some client sode javascript that woudl execute when client side validation failed. I haven't foudn any way of doing it - validators naturally dont' expose those "validation events" to hookup to. Of course I can create my custom validators but that would mean rewriting (and duplicating functionality) completely all existing asp.net validators which sounds a big chunk of work.
Any ideas?
Thank you in advance!
Andrey
Here is how I solved a similar issue. For every validator that is added to the accordion, I store the validator id in an array, along with the accordion panel that it is contained in. I then pass this list to the client. I also hook into the OnSubmit event by calling Page.ClientScript.RegisterOnSubmitStatement. The javascript function that executes during the onSubmit first checks to see if the page is valid, if so then it just exits, if not then it loops through the list of validators looking for ones that are not valid, when it finds one, it expands the section associated with that validator.

Categories