Knowledgebase Home Page  >  RapidSpell Web ASP.NET  >  Behavior Customization
Search the Knowledge Base
How can I show a form of data and highlight errors for the user to correct (programmatic or dynamic, force check)? (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=142&catId=55

Options

Print this page
Email this to a friend
The following example programmatically checks text pulled from a source (ArrayList) for errors and in cases where a string has an error, a textbox is shown with the RapidSpellWInline control used to highlight the errors.  This allows the user to correct the mistake.
 
To use the following, setup a project with our standard RSWIHelper.aspx page in it.
 
 
 
ASPX - WebForm1.aspx
 
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="RapidSpellWeb_Demo_CSharp.kiran.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
  </form>
 </body>
</HTML>
 
 
CODE BEHIND - WebForm1.aspx.cs
 
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 RapidSpellWeb_Demo_CSharp
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  int textFieldCounter = 0;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   ArrayList stringsFromDB = new ArrayList(new string[]{"sume errars", "no errors", "some moore errors", "some digits 12345", "no errors again", "same moore error"});
   
   AddFields(stringsFromDB);
   
  }
 
  void AddFields(ArrayList stringsFromDB){
   ArrayList spellcheckerIDs = new ArrayList();
 
   //Replace this license key with your own, or a current trial key.  This key was created at the time
   //of writing and may have expired.
   Keyoti.RapidSpell.RapidSpellChecker c = new Keyoti.RapidSpell.RapidSpellChecker("5752675E645A665F68596A69615A663839383D43383D3A3F413D4342434A4248474C528");
 
   foreach(string fromDB in stringsFromDB)
   {
    c.Check(fromDB);
 

    if(c.NextBadWord()!=null)//errors
    {
     SpellCheckField(fromDB, spellcheckerIDs);
    }
    else //none
    {
     ShowField(fromDB);
    }
    FindControl("Form1").Controls.Add(new LiteralControl("<br /><br />"));
   }
 
   Keyoti.RapidSpell.RapidSpellWebMultiple rswm = new Keyoti.RapidSpell.RapidSpellWebMultiple();
   rswm.ID="rswm";
   rswm.ShowFinishedMessage = false;
   rswm.RapidSpellWebLaunchers = new Keyoti.RapidSpell.RapidSpellWebLauncherCollection(String.Join(",", (string[])spellcheckerIDs.ToArray(typeof(string)) ));
   rswm.ShowButton = false;
   FindControl("Form1").Controls.Add(rswm);
 
   Page.RegisterClientScriptBlock("spellBoot", "<script>function runSpellCheckers(){"+rswm.ClientID+"_RunSpellCheck(true);} window.onload=runSpellCheckers;</script>");
 
   
  }
  
  void SpellCheckField(string text, ArrayList spellcheckerIDs)
  {
   Keyoti.RapidSpell.RapidSpellWInline rswi = null;
   TextBox dbText = new TextBox();
   dbText.TextMode = TextBoxMode.MultiLine;
   dbText.ID = "dbText_"+(textFieldCounter++);
   dbText.Text = text;
 
   rswi = new Keyoti.RapidSpell.RapidSpellWInline();
   rswi.RSMultipleID = "rswm";
   rswi.ID= "rswi_"+textFieldCounter;
   rswi.TextComponentID = dbText.ID;
   rswi.ShowButton = false;
    
   rswi.RapidSpellWInlineHelperPage = "rswihelper.aspx";
   FindControl("Form1").Controls.Add(dbText);
   FindControl("Form1").Controls.Add(rswi);
 
   spellcheckerIDs.Add(rswi.ClientID);
  }
 
  void ShowField(string text)
  {
   Label dbText = new Label();
   dbText.Text = text;
   FindControl("Form1").Controls.Add(dbText);
 
  }
 

  #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.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}
 
You can see that the RapidSpellWebMultiple control is hidden by the property rswi.ShowButton - this is to force the user to correct errors without skipping out.
 
 
Extending further
 
You can extend the example by adding a client script validator to the page, to check that all errors have been dealt with before continuing.
 
To the ASPX, add a postback button (if not already there)
 
<asp:Button id="Button1" runat="server" Text="Finish"></asp:Button>
 
and a CustomValidator

<asp:CustomValidator id="CustomValidator1"
    runat="server" ErrorMessage="Sorry, there are still errors." ClientValidationFunction="CheckForRemainingErrors"></asp:CustomValidator>
 
Note that the validator needs to call a JS function to check the number of errors.  So add a script block
 
  <script>
  function CheckForRemainingErrors(val, args){  
   var errorTally=0;
   for(var i=0; i<rsw_tbs.length; i++){
    errorTally += rsw_tbs[i].getNumberOfErrors();
   }
   args.IsValid = errorTally == 0;
  }
  </script>
 
This block just sums up the number of errors in each spell checker, and sets the IsValid flag true if there are zero.
 
 
Here's the modified ASPX page:
 
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="RapidSpellWeb_Demo_CSharp.kiran.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <script>
  function CheckForRemainingErrors(val, args){  
   var errorTally=0;
   for(var i=0; i<rsw_tbs.length; i++){
    errorTally += rsw_tbs[i].getNumberOfErrors();
   }
   args.IsValid = errorTally == 0;
  }
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Button id="Button1" runat="server" Text="Finish"></asp:Button>
   <asp:CustomValidator id="CustomValidator1"
    runat="server" ErrorMessage="Sorry, there are still errors." ClientValidationFunction="CheckForRemainingErrors"></asp:CustomValidator>
  </form>
 </body>
</HTML>
 
 
This is just one of many ways you can use RapidSpell to validate user input.  Please also see the example project that came with the product for simpler validator examples.

Related Questions:

Attachments:

No attachments were found.