-
Validating datagrid
I'm editing records in a datagrid (one at a time). Everytings works fine, but now I have to validate the input of the user.
First of all you have the client-side :
I tried the validation controls on the datagrid template columns. This worked, but the grid turned out extremly ugly because it has to allow room for the validation control as well as the textbox :cry: Is there an another way to do this f.e. outside the datagrid or what do experts do :D ?
And the server-side validation : how can this been performed ?
-
Re: Validating datagrid
Server-side validation can be performed using the DataGrid's UpdateCommand event - validate all the fields before performing any update actions. If the validation fails it quits the method and displays a message - either inside the DataGrid or in, for example, a label outside the DataGird.
Does that cover it? If you want any more detail post some code.
DJ
-
Re: Validating datagrid
Server-side : OK, will give it a try.
But what about my ugly grid ? Can the errormessage be displayed outside the grid ? Or do you have another solution for this problem ?
Thx for any help !
-
Re: Validating datagrid
Found the solution for my problem : put validation-controls in each template column I want to validate and set the property "display" to "none".
Outside my grid I placed a validationsummary-control.
This works fine, except that the error messages are only shown when I click on the Update-button and not immediatly when I leave/modify a textbox :cry:
All the "EnableClientScript" of my validation-controls are set to "True".
Why does this not work ??
-
Re: Validating datagrid
Because validation works onPostBack and clicking Update causes a PostBack - maybe try adding the OnTextChanged event to call the validation method - this could slow the page down some though.
DJ
-
Re: Validating datagrid
I have 2 questions for you :
- when I edit my template column, I can set the property AutoPostBack of my textbox = true. Why doesn't the validation work, even if there is a postback ?
- I cann't find any OnTextChanged event in my code-behind (textboxes of my template columns do not apear), so how can this been performed ?
:confused:
-
Re: Validating datagrid
You want the validation controls inside the same column as the control you are trying to validate against. You need to do is set the Display to Static, and leave enough spacing between the controls, so when the red * or ! appears, it doesn't push over the controls in that row. A spacing of 10px should be sufficient depending on the font size.
<templateColumn>
<asp:textbox runat="server" id="firstName" />
<asp:requiredfieldvalidator runat="server" id="requireFirstName" controlToValidate="firstName" errorMessage="First Name is required." display="static">*</asp:requiredfieldvalidator>
</templateColumn>
If the validator controls are not showing error messages after you leave the field in IE (they only work that way in IE), you probably are adding something to the 'onchange' event of the textboxes the validators are trying to validate.
If that is the case - respond and I will show you code that allows you to add your own onchange event and still works with the validation controls when you leave the field.
-
Re: Validating datagrid
Not really what I want... I would like to display the errormessages outside my datagrid. So I put a ValidationSummary-control outside my grid and put the properties of all the other validationcontrols as follows :
display = none
text = ""
This works fine, only the error-message(s) appear not directly (only when I click the update-button).
-
Re: Validating datagrid
The validation summary is only populated when you try submitting the form.
That is how it gets wired by the javascript the framework emits.
If you want to alert the user as they leave a field, you need to set the validators to a display other than 'None'.