Knowledgebase Home Page  >  RapidSpell Web ASP.NET  >  Behavior Customization
Search the Knowledge Base
How can I force spell check when SAVE is clicked? (C#, VB.NET)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=145&catId=55

Options

Print this page
Email this to a friend

Generally we prescribe the use of our controls as ASP.NET validators in situations where you want to ensure user's text is correctly spelled.  Validators are a very clean implementation, and conform to the standard ASP.NET way.  The validator examples in the demo project will show how.

 

If however you prefer to force the spell checker before saving, you can make the following Javascript modifications.  Note that these are required to run the spell check first, as it is asynchonous (i.e. launching the spell check and allowing the postback to occur at the same time would fail) and then do the postback.

 

 

1. Add this Javascript to the page;

 

<script>

function AutoCheckClass(startFunction){
 this.hasRun = false;
 this.startFunction = startFunction;
 this.runSpellCheck = runSpellCheck;
 this.saveButton;
 this.onFinish = onFinish;
 
 function onFinish(){  
  this.saveButton.click();
 }
 
 function runSpellCheck(saveButton){
  this.saveButton = saveButton;
  eval(this.startFunction);
  this.hasRun = true;
 }
}
</script>

 

this is for general reuse, and is wired up as follows.

 

2. Add this code to the script block

 

var leftAutoChecker = new AutoCheckClass("<%= RapidSpellWebMultiple1.ID %>_RunSpellCheck(true)");

 

function handleLeftSave(saveButton){
 if(leftAutoChecker.hasRun) return true;
 else {
  leftAutoChecker.runSpellCheck(saveButton);
  return false;
 }
}

 

Notice that it includes the string

 

"<%= RapidSpellWebMultiple1.ID %>_RunSpellCheck(true)"

 

this is used when checking with the RapidSpellWebMultiple control - if you prefer to use RapidSpellWebLauncher, change it to

 

"popUpCheckSpelling<%= RapidSpellWebLauncher3.ClientID %>('rsTCInt<%= RapidSpellWebLauncher3.ClientID %>')"

 

 

3. In the spell checker control, set

FinishedListener = "leftAutoChecker.onFinish"

this can be done in the ASPX or codebehind

4. In the codebehind, set the onclick client attribute for the Save button, eg.

SaveLeftButton.Attributes.Add("onclick", "return handleLeftSave(this);");

This ties up the loop so that the spell checker runs when the save is clicked, and the onFinish function reclicks the button after the spell check is finished.

 

Complete example

ASPX -------------------------------------------

 

<%@ Register TagPrefix="rapidspellweb" Namespace="Keyoti.RapidSpell" Assembly="Keyoti.RapidSpellWeb" %>
<%@ Page language="c#" Codebehind="ForceOnSave.aspx.cs" AutoEventWireup="false" Inherits="RSWEBCtrlTest.ForceOnSave" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>ForceOnSave</title>
  <script>
  //multiple - use this code for the multiple checker
  var leftAutoChecker = new AutoCheckClass("<%= RapidSpellWebMultiple1.ClientID %>_RunSpellCheck(true)");
  function handleLeftSave(saveButton){
   if(leftAutoChecker.hasRun) return true;
   else {
    leftAutoChecker.runSpellCheck(saveButton);
    return false;
   }
  }

  
  //single - use this code for the single checker
  var rightAutoChecker = new AutoCheckClass("popUpCheckSpelling<%= RapidSpellWebLauncher3.ClientID %>('rsTCInt<%= RapidSpellWebLauncher3.ClientID %>')");  
  function handleRightSave(saveButton){
   if(rightAutoChecker.hasRun) return true;
   else {
    rightAutoChecker.runSpellCheck(saveButton);
    return false;
   }
  }
  
  //shared - this code is required for either usage.
  function AutoCheckClass(startFunction){
   this.hasRun = false;
   this.startFunction = startFunction;
   this.runSpellCheck = runSpellCheck;
   this.saveButton;
   this.onFinish = onFinish;
   
   function onFinish(){  
    this.saveButton.click();
   }
   
   function runSpellCheck(saveButton){
    this.saveButton = saveButton;
    eval(this.startFunction);
    this.hasRun = true;
   }
  }
  </script>

 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <!-- LEFT (multiple) ================================== -->
   <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 253px" runat="server"></asp:TextBox>
   <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 333px" runat="server"></asp:TextBox>
   <RapidSpellWeb:RapidSpellWebLauncher id="RapidSpellWebLauncher1" style="Z-INDEX: 104; LEFT: 250px; POSITION: absolute; TOP: 338px"
    runat="server" TextComponentName="TextBox2" RapidSpellWebPage="popup.aspx" RSMultipleID="RapidSpellWebMultiple1"></RapidSpellWeb:RapidSpellWebLauncher>
   <rapidspellweb:RapidSpellWebLauncher id="RapidSpellWebLauncher2" style="Z-INDEX: 105; LEFT: 249px; POSITION: absolute; TOP: 257px"
    runat="server" TextComponentName="TextBox1" RapidSpellWebPage="popup.aspx" RSMultipleID="RapidSpellWebMultiple1"></rapidspellweb:RapidSpellWebLauncher>
   <rapidspellweb:RapidSpellWebMultiple id="RapidSpellWebMultiple1" style="Z-INDEX: 107; LEFT: 81px; POSITION: absolute; TOP: 421px"
    runat="server" RapidSpellWebLaunchers="RapidSpellWebLauncher1,RapidSpellWebLauncher2"></rapidspellweb:RapidSpellWebMultiple>
   <asp:Label id="Label1" style="Z-INDEX: 108; LEFT: 84px; POSITION: absolute; TOP: 203px" runat="server">Multiple</asp:Label>
   <asp:Button id="SaveLeftButton" style="Z-INDEX: 110; LEFT: 83px; POSITION: absolute; TOP: 545px"
    runat="server" Text="Save Left"></asp:Button>
   <!-- RIGHT (single) ================================== -->
   <asp:Button id="SaveRightButton" style="Z-INDEX: 111; LEFT: 564px; POSITION: absolute; TOP: 536px"
    runat="server" Text="Save Right"></asp:Button>
   <asp:TextBox id="TextBox3" style="Z-INDEX: 103; LEFT: 548px; POSITION: absolute; TOP: 256px"
    runat="server"></asp:TextBox>
   <rapidspellweb:RapidSpellWebLauncher id="RapidSpellWebLauncher3" style="Z-INDEX: 106; LEFT: 549px; POSITION: absolute; TOP: 304px"
    runat="server" TextComponentName="TextBox3" RapidSpellWebPage="popup.aspx"></rapidspellweb:RapidSpellWebLauncher>
   <asp:Label id="Label2" style="Z-INDEX: 109; LEFT: 548px; POSITION: absolute; TOP: 209px" runat="server">Single</asp:Label>
   
   <!--Shared-->
   <asp:Label id="Label3" style="Z-INDEX: 112; LEFT: 49px; POSITION: absolute; TOP: 61px" runat="server"
    Font-Size="16pt" Width="806px">In this example we demonstrate how to force a launcher or a multiple to run when a save button is clicked.  It's advised you look at the 'Left' or 'Right' code as applicable to your scenario.</asp:Label>
  </form>
 </body>
</HTML>

Code behind -------------------------------------------------------------------

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace RSWEBCtrlTest
{
 /// <summary>
 /// Summary description for ForceOnSave.
 /// </summary>
 public class ForceOnSave : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.TextBox TextBox2;
  protected System.Web.UI.WebControls.TextBox TextBox3;
  protected Keyoti.RapidSpell.RapidSpellWebLauncher RapidSpellWebLauncher1;
  protected Keyoti.RapidSpell.RapidSpellWebLauncher RapidSpellWebLauncher2;
  protected Keyoti.RapidSpell.RapidSpellWebLauncher RapidSpellWebLauncher3;
  protected Keyoti.RapidSpell.RapidSpellWebMultiple RapidSpellWebMultiple1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Button SaveLeftButton;
  protected System.Web.UI.WebControls.Button SaveRightButton;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label Label2;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   SaveLeftButton.Attributes.Add("onclick", "return handleLeftSave(this);");
   RapidSpellWebMultiple1.FinishedListener = "leftAutoChecker.onFinish";

   SaveRightButton.Attributes.Add("onclick", "return handleRightSave(this);");
   RapidSpellWebLauncher3.FinishedListener = "rightAutoChecker.onFinish";
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   this.SaveLeftButton.Click += new System.EventHandler(this.SaveLeftButton_Click);
   this.SaveRightButton.Click += new System.EventHandler(this.SaveRightButton_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void SaveLeftButton_Click(object sender, System.EventArgs e)
  {
   this.Label1.Text = "Left saved.";
  }

  private void SaveRightButton_Click(object sender, System.EventArgs e)
  {
   this.Label2.Text = "Right saved.";
  }
 }
}

 

VB.NET equivalent of page load


   ' Put user code to initialize the page here
   SaveLeftButton.Attributes.Add("onclick", "return handleLeftSave(this);")
   RapidSpellWebMultiple1.FinishedListener = "leftAutoChecker.onFinish"

   SaveRightButton.Attributes.Add("onclick", "return handleRightSave(this);")
   RapidSpellWebLauncher3.FinishedListener = "rightAutoChecker.onFinish"

 

Products Related to this Article

RapidSpell Web ASP.NET


Related Questions:

Attachments:

No attachments were found.