ASP.NET: Using Forms To Enter Data

Sorry about the long delay since the last article in which we covered how to add and edit database entries from within our custom "Animal" Class. I got distracted (see later). Now, let's add a form to let users actually add entries.

In Visual Studio add a new item to your project. Choose "Web Form based on Master Page" and call it "AnimalForm.aspx". Choose "Site.Master" as the template in the next dialog page.

You'll see a page like this one (comment is my own):


Notice the form comprises of two Content elements. These relate to areas in the Site.Master template and define where the . In the image above the Content element we put our form in is tied to a "place holder" called MainContent.

If you look in the Site.Master template you'll see a ContentPlaceHolder element with the ID of MainContent.


This ContentPlaceHolder inside a <div> with class "main". Anything you put in the second Content element on the Form's page will appear in this div.

Back in the AnimalForm.aspx page add some content, like so:


We've added a table with a row for name and type and one row for a Calendar item.

The form will look like this in a browser:


In practice I've never used a Calendar control, but thought I'd show you one in use.

Note that we don't need to add a form tag as there's already one in the Site.Master template.

Enter some values and press submit. Nothing happens. We need to add some logic to the AnimalForm.aspx.cs "code behind" file. Right click the ASPX file and choose "View Code". Now add a new method called SaveAnimal, like so:


Pretty simple, no? It just creates an animal. Assigns it values from the form elements we added and then saves it. At the point it's saved the Animal has an ID, so we can redirect to it.

Before this will work we need to switch back to the Form page and change the normal HTML submit button in to a Button element that points to our new method, like so:


Now you should be able to click save and the data is entered in the database.

I was going to go on and talk about how to create a link that lets you open an existing animal and edit it. But for reasons I'll get to, I don't think I will. In essence you simply create a link like /AnimalForm.aspx?id=4. This points to the same form as above but in the Page_Load event it would look for the ID parameter and - if it exists - load that Animal and fill the input boxes with its current values. Etc.

Classic ASP.NET Bad

Since starting this series of articles on (classic) ASP.NET I've fallen big time for both ASP.NET MVC 3 and Linq-to-SQL. The approach I'm showing you - of using DataSets and Stored Procedures - now seems like a nightmare in comparison. And I can't live with myself thinking I'm teaching you how to do it the "wrong" way.

What I plan on doing is finishing this "series" off by discussing a couple of things applicable to whatever approach you take -- such as setting up a server for deployment. Then I plan to talk about MVC and Linq-to-SQL. I think you'll like it.


    • avatar
    • Ferdy
    • Thu 24 Feb 2011 06:28 AM

    I can imagine your lack of motivation to finish this given your new enlightened path :)

    • avatar
    • Nick
    • Thu 24 Feb 2011 11:50 AM

    I am using the nightmare way....seems pretty simple to me...And I started exploring MVC, but I guess I need to learn more, so looking forward to your MVC articles too.

      • avatar
      • Jake Howlett
      • Thu 24 Feb 2011 03:49 PM

      Not saying classic/nightmare way isn't simple. It's just potentially very messy. It's a bit like Domino, which, as we all know is simple to build sites with, but, can be a nightmare to maintain or dig in to another person's code/way of working and figure out what on earth is going on.

      This week I'm working on a Domino website that has been started by another developer. It's a complete mess and a nightmare to figure out and pick up where it was left off.

      MVC adds a stronger pattern to development and Linq-to-sql removes the need (for the most part) for any stored procedures, so everything is just hunky dorey.

    • avatar
    • GCT
    • Mon 28 Feb 2011 01:47 AM


    • avatar
    • Ken
    • Thu 10 Mar 2011 01:06 PM

Your Comments


About This Page

Written by Jake Howlett on Thu 24 Feb 2011

Share This Page

# ( ) '


The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content