Results 1 to 9 of 9

Thread: Validating datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    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 Is there an another way to do this f.e. outside the datagrid or what do experts do ?

    And the server-side validation : how can this been performed ?

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    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 !

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    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

    All the "EnableClientScript" of my validation-controls are set to "True".

    Why does this not work ??

  5. #5
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

    If I have been helpful please rate my post. If I haven't tell me!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    Re: Validating datagrid

    I have 2 questions for you :

    1. 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 ?

    1. 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 ?



  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    94

    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).

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width