This example shows how to use DataItemTemplate and EditItemTemplate in Batch Edit mode. The templates use ASPxRatingControl as content.
Follow these steps:
1. Add ASPxGridView to your page and configure it as follows:
[ASPx]
<dx:ASPxGridViewID="ASPxGridView1"runat="server"KeyFieldName="ID"ClientInstanceName="grid"DataSourceID="ObjectDataSource1">
...<SettingsEditingMode="Batch"><BatchEditSettingsEditMode="Cell"AllowRegularDataItemTemplate="true"StartEditAction="FocusedCellClick"/></SettingsEditing><ClientSideEventsBatchEditStartEditing="OnBatchEditStartEditing"BatchEditEndEditing="OnBatchEditEndEditing"FocusedCellChanging="OnFocusedCellChanging"Init="OnGridInit"/></dx:ASPxGridView>
Note the values of the AllowRegularDataItemTemplate and StartEditAction properties.
2. Add controls to the DataItemTemplate and EditItemTemplate. Since DataItemTemplate generates multiple instances of a control, the control's ClientInstanceName need to be set dynamically. ClientInstanceName for the control in EditItemTemplate is set statically.
[ASPx]
<DataItemTemplate><dx:ASPxRatingControlID="ratingControl"runat="server"ClientInstanceName='<%# "DataItemRateControl"+ Container.VisibleIndex %>'ItemCount="5"Value='<%# Convert.ToInt32(Eval("RatingValue")) %>'><ClientSideEventsItemClick="OnItemMouseClick_DataItem"ItemMouseOver="OnItemMouseOver_DataItem"ItemMouseOut="OnItemMouseOut_DataItem"/></dx:ASPxRatingControl></DataItemTemplate><EditItemTemplate><dx:ASPxRatingControlID="ratingControl"runat="server"ClientInstanceName="EditItemRateControl"ItemCount="5"><ClientSideEventsItemClick="OnItemMouseClick_EditItem"Init="OnRateControlInit_EditItem"/></dx:ASPxRatingControl></EditItemTemplate>
3. Add handler for the client-side
BatchEditStartEditing event to set the ASPxRatingControl value inside the EditItemTemplate when editing starts. To save the selected value in the grid when editing ends, use
OnBatchEditEndEditing.
function OnBatchEditStartEditing(s, e) {
EditItemRateControl.SetValue(e.rowValues[s.GetColumnByField("RatingValue").index].value);
}
function OnBatchEditEndEditing(s, e) {
var templateColumn = s.GetColumnByField("RatingValue");
if (!e.rowValues.hasOwnProperty(templateColumn.index))
return;
var cellInfo = e.rowValues[templateColumn.index];
cellInfo.value = EditItemRateControl.GetValue();
SetRateControlValueByRowIndex_DataItem(e.visibleIndex, cellInfo.value);
}
4. Add the client-side ItemMouseClick handler for controls in EditItemTemplate and DataItemTemplate separately.
When the EditItemTemplate's ASPxRatingControl item is clicked, finish editing with the
batchEditApi.EndEdit method. When the event occurs in ASPxRatingControl from DataItemTemplate, set a cell value with the
batchEditApi.SetCellValue method.
function OnItemMouseClick_EditItem(s, e) {
grid.batchEditApi.EndEdit();
}
function OnItemMouseClick_DataItem(s, e) {
grid.batchEditApi.SetCellValue(currentFocusedCell.itemVisibleIndex, currentFocusedCell.column.index, s.GetValue());
}
5. Add keyboard navigation to the grid. Handle the ASPxClientGridView.Init event and subscribe to the keydown event there. Then, process different key codes in this handler:
function OnGridInit(s, e) {
ASPxClientUtils.AttachEventToElement(s.GetMainElement(), "keydown", function (evt) {
return OnKeyDown(evt, s);
});
}
function OnGridViewKeyDown(evt, grid) {
if (typeof (event) != "undefined" && event != null)
evt = event;
if (!grid.InCallback() && NeedProcessDocumentKeyDown(evt)) {
if (evt.shiftKey && evt.keyCode == 9 /*Shift + tab */) {
setTimeout(function () {
grid.batchEditApi.MoveFocusBackward();
}, 0);
} else if (evt.keyCode == 9 /*Tab key*/) {
setTimeout(function () {
grid.batchEditApi.MoveFocusForward();
}, 0);
}
}
}
6. Use the approach from p.5 to add keyboard support in ASPxRatingControl:
function OnRateControlInit_EditItem(s, e) {
ASPxClientUtils.AttachEventToElement(s.GetMainElement(), "keydown", function (evt) {
return OnRatingControlKeyDown(evt, s);
});
}
function OnRatingControlKeyDown(evt, ratingControl) {
if (typeof (event) != "undefined" && event != null)
evt = event;
if (!ratingControl.InCallback() && NeedProcessDocumentKeyDown(evt)) {
if (evt.keyCode == 32 /*Space bar*/) {
setTimeout(function () {
MoveFocusToNextStar();
}, 0);
}
}
}
Files to look at:
•
Default.aspx (VB:
Default.aspx)
•
Default.aspx.cs (VB:
Default.aspx.vb)
•
BatchEditController.js (VB:
BatchEditController.js)