|5. WebForms Lab|
Overview: you'll create a WebForms application that does spell checking. Note that IIS must be installed for this lab assignment.
A WebForms Application for Spell Checking
The web app you're going to build will do spell checking against the 9th edition of the English Merriam-Webster dictionary. A text file is provided of approximately 200,000 words, one per line, in sorted order. The plan is to read this into an ArrayList data structure, which you can then search using binary search.
Startup Visual Studio and create a new C# project of type ASP.NET Web Application. When asked for the web app's location, change the application name to "SpellCheck". Visual Studio will then configure IIS with a virtual directory named SpellCheck, mapped to the physical directory C:\Inetpub\wwwroot\SpellCheck\. Next, rename the "WebForm1.aspx" file (via the Solution Explorer window of Visual Studio) to "default.aspx". This way, the user will surf to your WebForm by default.
Before we forget, make a copy of the dictionary file "merriam-webster.txt" provided in the work directory (within the Labs directory "5-WebApps"), and place a copy of this file in the directory that Visual Studio created for your web app: this should be "C:\Inetpub\wwwroot\SpellCheck\".
Place 2 labels, a text box, and a button on the WebForm as shown. Program the page's Load event to open the file and read the words into an ArrayList data structure; the ArrayList variable will need to be a private field of the page class so that it can be accessed by the button's Click event. For File I/O, see the lecture on the FCL. When you open the file, you'll need the full pathname. You can hard-code as @"C:\Inetpub\wwwroot\SpellCheck\merriam-webster.txt", or compute it dynamically as follows:
string path, filename;
path = System.AppDomain.CurrentDomain.BaseDirectory;
filename = path + @"\merriam-webster.txt";
Once the Load event is coded to fill the ArrayList, code the spell check button to search for the given word using the BinarySearch() method of the ArrayList class. Update the label on the form to inform the user as to whether or not the word was spelled correctly. Run and test!
Due to the client-server model of web-based applications, note that each time the button is clicked, this results in another page request to the web server. This in turn yields another Page_Load event, which in turn causes the file to be reopened, another ArrayList filled, etc. Not very efficient.
One possible solution is to open and process the dictionary file only upon the first request; on subsequent requests (known as "postbacks"), we should be able to reuse the previous ArrayList object. Try this by modifying the Page_Load event so that if this event is a postback, you immediately return from the event (in other words, do nothing). Run and test. What happens? Can you explain this behavior?
Ahhh, what happened is one of the subtle differences between web programming and traditional Windows programming. By default the web server is stateless, state is not maintained from one request to another. Thus, on the first request the file is opened and the ArrayList filled. But then, on each subsequent request, a new page object is created, the previous state (ArrayList object) is lost, and hence the NullReferenceException.
One solution is to use ASP.NET's Session state object to keep track of the ArrayList object for the duration of the user's session. This is easily done by storing a reference to the ArrayList object in Session state as follows:
this.Session["words"] = <<reference to ArrayList object>>;
Then, on a postback, the Page_Load event should just retrieve the reference from Session state. As an alternative solution, you can code the Page_Load event to do nothing on a postback, and then code the button's Click event to always retrieve the ArrayList reference from Session state. Either way, run and test. Now you have a more efficient web-based application!
Just in case you are wondering, ASP.NET also supports the notion of Application state, which can even be shared amongst a set of web servers (often called a "web farm")...