AxpDataGrid

Server Side Input Validation

The recommended method for performing Server Side Input Validation is to create event handlers for the AxpDataGrid.BeforeInsertRecord and AxpDataGrid.BeforeUpdateRecord events.

In the event handler check the posted input values using the AxpDataGrid1.UpdateFields collection.

If the validation fails, call the AxpDataGrid1.AbortCommandExecution() method.

A warning message can be displayed next to the input field using the AxpDataGrid.SetEditField method.

[Visual Basic]
  Private Sub AxpDataGrid1_BeforeInsertRecord(ByVal sender As Object, ByVal e As Axezz.WebControls.UpdateRecordEventArgs) Handles AxpDataGrid1.BeforeInsertRecord
    ValidateFields()
  End Sub

  Private Sub AxpDataGrid1_BeforeUpdateRecord(ByVal sender As Object, ByVal e As Axezz.WebControls.UpdateRecordEventArgs) Handles AxpDataGrid1.BeforeUpdateRecord
    ValidateFields()
  End Sub

  Private Sub ValidateFields()
    If Not AxpDataGrid1.UpdateFields("QuantityPerUnit") Is Nothing Then
      If AxpDataGrid1.UpdateFields("QuantityPerUnit").ColumnValue.Trim.Length > 0 Then
        Dim test As Integer = -1
        Try
          test = Integer.Parse(AxpDataGrid1.UpdateFields("QuantityPerUnit").ColumnValue)
        Catch
          AxpDataGrid1.AbortCommandExecution()
          Dim edit1 As New Axezz.WebControls.EditFieldItem
          With edit1
            .ColumnName = "QuantityPerUnit"
            .HtmlAfter = "<font color='red'><strong> Not a number</strong></font>"
          End With
          AxpDataGrid1.SetEditField(edit1)
        End Try
      End If
    End If
  End Sub
[C#]
    private void InitializeComponent()
    {    
      this.AxpDataGrid1.BeforeUpdateRecord += new Axezz.WebControls.AxpDataGrid.BeforeUpdateRecordEventHandler(this.AxpDataGrid1_BeforeUpdateRecord);
      this.AxpDataGrid1.BeforeInsertRecord += new Axezz.WebControls.AxpDataGrid.BeforeInsertRecordEventHandler(this.AxpDataGrid1_BeforeInsertRecord);
      this.Load += new System.EventHandler(this.Page_Load);
    }

    private void ValidateFields()
    {
      if (AxpDataGrid1.UpdateFields["QuantityPerUnit"] != null)
        if (AxpDataGrid1.UpdateFields["QuantityPerUnit"].ColumnValue.Trim().Length > 0)
        {
          int test=-1;
          try
          {
            test = int.Parse(AxpDataGrid1.UpdateFields["QuantityPerUnit"].ColumnValue);
          }
          catch
          {
            AxpDataGrid1.AbortCommandExecution();
            // Show error message below the input text field.
            Axezz.WebControls.EditFieldItem efi = new Axezz.WebControls.EditFieldItem();
            efi.ColumnName = "QuantityPerUnit";
            efi.HtmlAfter = "<font color='red'><strong> Must be a number</strong></font>";
            AxpDataGrid1.SetEditField(efi);
          }
        }
    }

    private void AxpDataGrid1_BeforeInsertRecord(object sender, Axezz.WebControls.UpdateRecordEventArgs e)
    {
      ValidateFields();
    }

    private void AxpDataGrid1_BeforeUpdateRecord(object sender, Axezz.WebControls.UpdateRecordEventArgs e)
    {    
      ValidateFields();
    }

Remarks

See also Sample25 in the Sample application.

Client side validation and ASP.NET Validation Controls is not supported by AxpDataGrid.

See Also

AxpDataGrid Tutorial