This demo illustrates how to spell check text input fields in RadGrid's edit form using RadSpell. This is useful when you would like to notify the end user when there are spelling mistakes in the text editor fields he/she uses to update/insert grid records.
The key moments are:
Attach a client script to the "Update" button in the grid's edit form (using asp HiddenField to store its id for this purpose).
Take advantage of RadSpell.ControlsToCheck property, add the ClientID's of all the textbox editors that should be checked.
Call the startSpellCheck() client method of RadSpell to check the specified input controls.
Raise a flag when the check is finished to update/insert the data.
Additionally, the grid and spell components are ajaxified using RadAjaxManager instance to perform the data editing operations with asynchronous requests. Here are the relevant code snippets from the online demo:
JavaScript
<telerik:RadCodeBlockID="RadCodeBlock1" runat="server">//boolean flag which determines whether the spell check has been processed or not varIsChecked=false;//starts the spell check operation functionStartCheck(){if(!IsChecked){var spell =$find('<%= RadSpell1.ClientID %>');
spell.startSpellCheck();returnfalse;}else{returntrue;}}functionSpellCheckFinished(sender, args){IsChecked=true;}functionSpellCheckClosed(sender, args){if(IsChecked){//trigger submit from the update/insert button in the grid //the id of the update or insert button is extracted from a hidden field var buttonID =document.getElementById('<%=HiddenField1.ClientID %>').value;document.getElementById(buttonID).click();IsChecked=false;}}</telerik:RadCodeBlock>
protectedvoidRadGrid1_ItemCreated(object sender,GridItemEventArgs e){if(e.Item isGridEditableItem&& e.Item.IsInEditMode){GridEditableItem editedItem = e.Item asGridEditableItem;GridEditManager editMan = editedItem.EditManager;ImageButton actionButton;//insert mode if(editedItem.OwnerTableView.IsItemInserted){
actionButton = editedItem.FindControl("PerformInsertButton")asImageButton;}//edit modeelse{
actionButton = editedItem.FindControl("UpdateButton")asImageButton;}
actionButton.OnClientClick ="return StartCheck();";
HiddenField1.Value = actionButton.ClientID;Stack controlsToCheck =newStack();int index =0;foreach(GridColumn column in editedItem.OwnerTableView.RenderColumns){if(column isIGridEditableColumn&& column.IsEditable){IGridColumnEditor editor = editMan.GetColumnEditor(column asIGridEditableColumn);if(editor isGridTextBoxColumnEditor){string editorID =(editor asGridTextBoxColumnEditor).TextBoxControl.ClientID;
controlsToCheck.Push(editorID);
index++;}}}
RadSpell1.ControlsToCheck =newstring[index];while(controlsToCheck.Count >0){
RadSpell1.ControlsToCheck.SetValue(controlsToCheck.Pop(), controlsToCheck.Count);}
RadSpell1.IsClientID =true;}}protectedvoidRadGrid1_ColumnCreated(object sender,GridColumnCreatedEventArgs e){//make the product id column read-only if(e.Column.IsBoundToFieldName("ProductID")){(e.Column asGridBoundColumn).ReadOnly =true;}}protectedvoidRadGrid1_ItemUpdated(object source,Telerik.Web.UI.GridUpdatedEventArgs e){GridEditableItem item =(GridEditableItem)e.Item;String id = item.GetDataKeyValue("ProductID").ToString();if(e.Exception !=null){
e.KeepInEditMode =true;
e.ExceptionHandled =true;SetMessage("Product with ID "+ id +" cannot be updated. Reason: "+ e.Exception.Message);}else{SetMessage("Product with ID "+ id +" is updated!");}}protectedvoidRadGrid1_ItemInserted(object source,GridInsertedEventArgs e){if(e.Exception !=null){
e.ExceptionHandled =true;SetMessage("Product cannot be inserted. Reason: "+ e.Exception.Message);}else{SetMessage("New product is inserted!");}}privatevoidSetMessage(string message){
lblMessage.Text = message;}
VB
ProtectedSub RadGrid1_ItemCreated(ByVal sender AsObject,ByVal e As GridItemEventArgs)Handles RadGrid1.ItemCreated
IfTypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode ThenDim editedItem As GridEditableItem =TryCast(e.Item, GridEditableItem)Dim editMan As GridEditManager = editedItem.EditManager
Dim actionButton As ImageButton
'insert mode If editedItem.OwnerTableView.IsItemInserted Then
actionButton =TryCast(editedItem.FindControl("PerformInsertButton"), ImageButton)Else'edit mode
actionButton =TryCast(editedItem.FindControl("UpdateButton"), ImageButton)EndIf
actionButton.OnClientClick ="return StartCheck();"
HiddenField1.Value = actionButton.ClientID
Dim controlsToCheck AsNew Stack()Dim index AsInteger=0For Each column As GridColumn In editedItem.OwnerTableView.RenderColumns
IfTypeOf column Is IGridEditableColumn AndAlso column.IsEditable ThenDim editor As IGridColumnEditor = editMan.GetColumnEditor(TryCast(column, IGridEditableColumn))IfTypeOf editor Is GridTextBoxColumnEditor ThenDim editorID AsString=TryCast(editor, GridTextBoxColumnEditor).TextBoxControl.ClientID
controlsToCheck.Push(editorID)
index +=1EndIfEndIfNext
RadSpell1.ControlsToCheck =NewString(index -1){}While controlsToCheck.Count >0
RadSpell1.ControlsToCheck.SetValue(controlsToCheck.Pop(), controlsToCheck.Count)EndWhile
RadSpell1.IsClientID =TrueEndIfEndSubProtectedSub RadGrid1_ColumnCreated(ByVal sender AsObject,ByVal e As GridColumnCreatedEventArgs)Handles RadGrid1.ColumnCreated
'make the product id column read-onlyIf e.Column.IsBoundToFieldName("ProductID")ThenTryCast(e.Column, GridBoundColumn).ReadOnly=TrueEndIfEndSubProtectedSub RadGrid1_ItemUpdated(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridUpdatedEventArgs)Handles RadGrid1.ItemUpdated
Dim item As GridEditableItem =DirectCast(e.Item, GridEditableItem)Dim id As [String] = item.GetDataKeyValue("ProductID").ToString()If e.Exception IsNotNothingThen
e.KeepInEditMode =True
e.ExceptionHandled =True
SetMessage("Product with ID "+ id +" cannot be updated. Reason: "+ e.Exception.Message)Else
SetMessage("Product with ID "+ id +" is updated!")EndIfEndSubProtectedSub RadGrid1_ItemInserted(ByVal source AsObject,ByVal e As GridInsertedEventArgs)Handles
RadGrid1.ItemInserted()If e.Exception IsNotNothingThen
e.ExceptionHandled =True
SetMessage("Product cannot be inserted. Reason: "+ e.Exception.Message)Else
SetMessage("New product is inserted!")EndIfEndSubPrivateSub SetMessage(ByVal message AsString)
lblMessage.Text= message
EndSub