$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
GridView Validation in ASP.NET AJAX
Home > How to > Grid Validation

Validation Field

In many cases, there is a need to validate the input data is correct. So KAX.Grid provide a validator for users. While using a validator, you need to check the grid column field type. Validator can be only working in GridBoundColmn, GridNumericColumn, GridHTMLEditorColumn, GridDropDownColumn and GridDateTimeColumn.
In ColumnValidationSettings property, you can set the RequiredFieldValidator to specify the error message content and the text color.
ASPX code:

<kettic:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
<ColumnValidationSettings EnableRequiredFieldValidation="true" EnableModelErrorMessageValidation="true">
<RequiredFieldValidator ForeColor="Red" ErrorMessage="This field is required"></RequiredFieldValidator>
<ModelErrorMessage BackColor="Red" />
</ColumnValidationSettings>
</kettic:GridBoundColumn>

Add a Validator

In order to check the insert/update data is correct format, you can add a validator to the grid. It's really very simple, just follow the four steps in ItemCreate event as below:
  • Make sure the current item is in edit mode.
  • Obtain the GridColumnEditor instance.
  • Create a validator and connect it to the editing control by ControlToValidate property.
  • Add the validator to the controls of editing control’s parent.
C# code:

protected void KAXGrid1_ItemCreated(object sender, kettic.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = e.Item as GridEditableItem;
GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("ContactName");
TableCell cell = (TableCell)editor.TextBoxControl.Parent;

RequiredFieldValidator validator = new RequiredFieldValidator();

validator.ControlToValidate = editor.TextBoxControl.ID;
validator.ErrorMessage = "*";
cell.Controls.Add(validator);
}
}





Specify in Server-side Validation event

In ServerValidation event, you can set the validation rules to the input data. If the input data is not validated, you can cancel this change.
ASPX code:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DataItem.Country") %>'>
</asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid input"
ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>





C# code:

protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if (args.Value.StartsWith("X"))
{
args.IsValid = false;
}
}
protected void KAXGrid1_ItemCommand(object source, kettic.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == KAXGrid.UpdateCommandName)
{
if (!Page.IsValid)
{
e.Canceled = true;
}
}
}
ASP.NET AJAX UI Controls