
|
Building an ASP.NET Note Taking Application By John Peterson | |
Rating: 2.9 out of 5 Rate this article
|
email this article to a colleague
suggest an article
|
download source code
Introduction
I've worked with computers and technology pretty much all my life and can't really imagine
running a business without them. That being said... when it comes to my personal life,
I never seem to learn. Before I get to the code, let me take a minute and relate the
inspiration behind this little application.
One of my friends was out of town and needed me to take care of some things at his place.
Most of it was relatively straight-forward, but there was one thing which was relatively
complex and I knew I would need to jot down some notes. So we chose a good time and I called
my friend to get the instructions which I proceeded to write down on a piece of scrap paper.
Well I'm sure you've already guessed what happened... I got to his apartment and naturally, my notes
were sitting on my desk at home. The worst part is that both our places are wired to the hilt. If I had
simply typed the notes into my computer instead of writing them down on paper I could have logged
in remotely and gotten them. Instead I was left with the choice of driving back to my place to get
the notes (and then making another round trip to his place) or calling my friend and having him
walk me through the process again. Neither choice was very appealing.
When I got home, I wrote a note to remind myself that I should build a little note
talking application. It's nothing much from a technology point of view, but sometimes
it's the simple things that help you the most. (I'm going to take this opportunity to
spread the word about the Alt-D and Ctrl-Enter key combinations of most modern browsers.
Try them for a week and you won't be able to stop!) Anyway... here's the result of my
frustrations... a one-page, stupidly simple, online note taking application. I wrote
it mainly for myself, but if it's something you can use then please take it and use it.
The Concept and UI
The idea was to keep things as simple as possible. To that end, the data is simply
stored in text files on the file system. I could just have used a database, but this
seemed simpler and I also liked the fact that if push came to shove I could always just
download the text files to a flash drive and take them with me. The downside to this
approach is that you'll need to have NTFS permissions to read and write to the notes folder.
The interface is as simple as I could get it. At the top of the page is a DropDownList
which lists the notes that currently exist. Selecting a note from the list loads the
note name into a TextBox and the contents of the note into a TextArea. I included
a blank entry as the first item in the DropDownList. Selecting it empties the TextBox
and TextArea making it easy to create a new note.
Editing a note is straight-forward. Load the note, make your changes, and click the
"Save Note" button. Creating a new copy of an existing note is easy as
well. Simply load the existing note and change the note's name in the TextBox before
clicking the "Save Note" button. The original note remains and a new note
is created with the new name.
There is no simple process for renaming a note. It's not technically difficult, but it would have muddled up
the UI a little so if you want to rename a note you're stuck creating a new one and deleting the old.
Which brings us to deleting a note. Simply select the note to delete in the DropDownList and click the
"Delete Note" button. Please be careful though... there's no confirmation step or undelete.
Once you delete a note, it's gone.
The Code
The code is relatively straight-forward and I commented it pretty well so I won't
spend too much time discussing it here.
The only thing really worth mentioning is that I am using AutoPostBack on the DropDownList to
eliminate a "Load Note" button that would otherwise be needed.
<%@ Page Language="VB" Strict="true" validateRequest="false" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' When the page first loads, fill the DropDownList
' with the list of existing notes.
If Not Page.IsPostBack Then
LoadNoteList("")
End If
End Sub
Protected Sub ddlNotes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Set the value of the note name textbox.
txtNoteName.Text = ddlNotes.SelectedValue
' If no note is selected clear the textarea, otherwise
' load it with the contents of the selected note.
If ddlNotes.SelectedValue = "" Then
txtNoteText.InnerText = ""
Else
txtNoteText.InnerText = ReadNoteFromFile(ddlNotes.SelectedValue)
End If
End Sub
Protected Sub btnDeleteNote_Click(ByVal sender As Object, ByVal e As System.EventArgs)
DeleteNoteFile(ddlNotes.SelectedValue)
' Reload the note list and set to the default selection
' since the current note no longer exists.
LoadNoteList("")
End Sub
Protected Sub btnSaveNote_Click(ByVal sender As Object, ByVal e As System.EventArgs)
WriteNoteToFile(txtNoteName.Text, txtNoteText.InnerText)
' Reload the note list to include the new note we may have
' just created. The parameter specifies that the note just
' saved should be made the selected note in the DropDownList.
LoadNoteList(txtNoteName.Text)
End Sub
Sub LoadNoteList(ByVal strSelectedNote As String)
Dim I As Integer
Dim arrFiles() As FileInfo
Dim myDirInfo As New DirectoryInfo(Server.MapPath("notes/"))
Dim liSelected As ListItem
' Get a list of all .txt files in the notes folder.
arrFiles = myDirInfo.GetFiles("*.txt")
' Clear the list and repopulate. The first entry is blank.
' The others each correspond to a .txt file in the notes folder.
ddlNotes.Items.Clear()
ddlNotes.Items.Add("")
For I = LBound(arrFiles) To UBound(arrFiles)
ddlNotes.Items.Add(Replace(arrFiles(I).Name.ToString, ".txt", ""))
Next I
' If a note/file is loaded make that one the selected entry.
liSelected = ddlNotes.Items.FindByText(strSelectedNote)
If Not liSelected Is Nothing Then liSelected.Selected = True
End Sub
Sub WriteNoteToFile(ByVal strNoteName As String, ByVal strNoteText As String)
Dim objStreamWriter As StreamWriter
' Get a handle on the file to write to
' and connect it to the StreamWriter object
objStreamWriter = File.CreateText(Server.MapPath("notes/" & strNoteName & ".txt"))
' Write text
objStreamWriter.Write(strNoteText)
' Flush to disk and close file
objStreamWriter.Flush()
objStreamWriter.Close()
End Sub
Function ReadNoteFromFile(ByVal strNoteName As String) As String
Dim objStreamReader As StreamReader
Dim strFileContents As String
' Get a handle on the file to read from
' and connect it to the StreamReader object
objStreamReader = File.OpenText(Server.MapPath("notes/" & strNoteName & ".txt"))
' Read the whole file and close the StreamReader
strFileContents = objStreamReader.ReadToEnd()
objStreamReader.Close()
' Set the return value of our function
ReadNoteFromFile = strFileContents
End Function
Sub DeleteNoteFile(ByVal strNoteName As String)
' Delete the appropriate note file
File.Delete(Server.MapPath("notes/" & strNoteName & ".txt"))
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Notepad</title>
</head>
<body>
<form id="myForm" runat="server">
<asp:DropDownList ID="ddlNotes" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlNotes_SelectedIndexChanged"
/>
<asp:Button ID="btnDeleteNote" Text="Delete Note" runat="server"
OnClick="btnDeleteNote_Click"
/>
<br /><br />
Note Name: <asp:TextBox ID="txtNoteName" runat="server" />
<asp:Button ID="btnSaveNote" Text="Save Note" runat="server"
OnClick="btnSaveNote_Click"
/><br />
<TextArea id="txtNoteText" cols="80" rows="30" runat="server" />
</form>
</body>
</html>
If you prefer, you can download a zip file containing the code from here:
070329.zip.
Conclusion
As I said earlier, sometimes it's the little things in life that can make your day. Is this little application
going to solve all your problems? No. Will it quickly and easily record notes and let you access them from
anywhere? It might... you just need to use it. For my part... I'm adding a link to it to my browser's Links
toolbar right now.
|
|
|
|
|
|
|
Other Articles
|
|
|
|
|
Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
|
|
|
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
[Read This Article] [Top]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
|
|
|
With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
[Read This Article] [Top]
|
|
|
|
Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
|
|
|
Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
[Read This Article] [Top]
|
|
|
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.
|
|