Building a Note-Taking App in ASP.NET

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn Building a Note-Taking App in ASP.NET

Requirements

  • ASP.NET

Difficulty

  • Intermediate

Building a Note-Taking App in ASP.NET

The idea is 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>

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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

Plagiarised from here.

You can contact us on Discord.
[utopian-moderator]

Congratulation

Today one year ago you joined SteemIt
Thank you, for making SteemIt great and Steem on for more years to come!

(You are being celebrated here)

Congratulations @carver! You have received a personal award!

1 Year on Steemit
Click on the badge to view your own Board of Honor on SteemitBoard.

Upvote this notificationto to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64038.60
ETH 3148.89
USDT 1.00
SBD 3.97