Grid View for ASP.NET Web Forms - How to validate entered cell values on the server in batch edit mode
This example demonstrates how to pass an editor's value to the server in batch edit mode, validate the value based on a condition, and pass the validation result to the client.
Create the Grid View control and add a callback control to the page. For particular grid columns, specify their edit settings and handle client-side TextChanged events. In the handler, get an editor's value and call the callback's PerformCallback method to pass the value to the server.
var editor;
function OnTextChanged(s, e) {
editor = s;
clb.PerformCallback(fieldName + '|' + s.GetText());
// ...
}
<dx:ASPxGridView ID="Grid" ClientInstanceName="grid" runat="server" KeyFieldName="ID" ... >
<Columns>
<!-- ... -->
<dx:GridViewDataTextColumn FieldName="C3">
<PropertiesTextEdit>
<ClientSideEvents TextChanged="OnTextChanged" KeyDown="OnKeyDown" />
<!-- ... -->
</PropertiesTextEdit>
</dx:GridViewDataTextColumn>
<!-- ... -->
</Columns>
<SettingsEditing Mode="Batch" />
</dx:ASPxGridView>
<dx:ASPxCallback ID="ASPxCallback1" runat="server" ClientInstanceName="clb" OnCallback="ASPxCallback1_Callback">
<ClientSideEvents CallbackComplete="OnCallbackComplete" />
</dx:ASPxCallback>
Handle the callback's server-side Callback event. In the handler, specify error text strings for invalid cells based on a condition and save these text strings to the e.Result argument property. Then pass the result to the callback's client-side CallbackComplete event to complete validation on the client.
var isError = false;
function OnCallbackComplete(s, e) {
// ...
if (e.result != null) {
grid.batchEditApi.StartEdit(currentIndex, grid.GetColumnByField(fieldName).index);
editor.SetIsValid(false);
editor.SetErrorText(e.result);
isError = true;
}
else
isError = false;
}
protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e) {
string[] parameters = e.Parameter.Split('|');
switch (parameters[0]) {
case "C3":
if (parameters[1] == "AAA")
e.Result = string.Format("'{0}' is invalid value", parameters[1]);
break;
case "C5":
if (parameters[1] == "BBB")
e.Result = string.Format("'{0}' is invalid value", parameters[1]);
break;
default:
break;
}
// ...
}
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
(you will be redirected to DevExpress.com to submit your response)