Custom Search

ASP.NET web application

For Beginners: Your are about to take a journey into the vast, complex world of web development with ASP.NET. ASP.NET was created by Microsoft to make the Internet more user friendly. As you explore this session you will learn that controls such as labels, textboxes, and buttons can be easily generated. Then server commands control something called "Code Behind" that allows more advanced programming such as Object Oriented. Object Oriented is actually a programming language that takes its roots from the Visual C language.

With ASP.NET you have the selection of programming in either Visual Basic.NET, Visual C#, or Visual J. However since Microsoft C# is more of the standard these days, I have decided to focus efforts on it instead.

Listed below is a great book I own called Microsoft ASP.NET 4 Step by Step. I am personally recommending this book if you want to gain a deeper understanding of the 4.0 framework. I realize there is the 4.5 framework, but you can still learn a lot from this book, I promise. This website is a direct result of the experience I have obtained from books like these. It will teach you how to create your own custom controls, write your own Master Pages, learn how to configure Internet Information Services to your preference, what Data Binding is used for (examples GridView, DataBind, FormView, etc.).

You will also learn how to setup web.config to your own configuration. This can be helpful if you only want certain users to access your pages. In Chapter 14 you will gain a solid understanding of using sessions in code. Sessions recall data from previous pages. There are also examples on mastering caching. Chapter 17 deals with Debugging techniques. Chapter 21 explores Windows Presentation Foundation (WPF) and Chapter 19 discusses MVC (Model View Controller). MVC is helpful when you want to quickly manage web sites without writing a lot of code. I have written a tutorial on my website for this based off the principles learned in this book. Chapter 23 explains AJAX which is used to provide faster loading of components within your website. Then Silverlight is devoted to Chapter 24 and Windows Communication Foundation (WCF) information in Chapter 25. Finally you can deploy your applications into production following the process found in Chapter 26. There are 602 pages in all.

To learn more about this book, click on the book image Microsoft ASP.NET 4 on the left.

How to setup your first project

To get started I am assuming you already know how to create an ASP.NET project. I can give you a quick review if you don't know how to do this. Open up Visual Studio (2008/2010) or you can use Visual Web Developer. Click on the File - New - Web Site. At the bottom where it says Web location choose a website name for your project. Make sure that ASP.NET Web Site is selected. For the installed templates you will choose Visual C# (under Recent Templates). You have now created your first web project.

If you are familiar with programming it is a commonality that each beginner should learn the Hello World example. The Hello world program consists of only a few things and shows some output on the screen. This is so you can instantly get a taste of how a simple web application works.

The first example will be just to get a taste of how something works on a web server. Then after that I will move onto creating a basic page (no design/templates, etc).

Look at this example below:

Here is an example Hello World program

building a web application

The example below shows what the program will look like when run through a browser. When the button Click me is pressed, the Label2 will be filled with the message Hello, World! The Label2 is actually what is known as a label and text can be printed to it. Click the Next button to see the C# code.

csharp

ASP.NET Labels

c# classes

Code view

When you right click in Visual Studio and select View Code the editor will take you to the section where all the code is produced behind scenes. Please note that this lesson will not attempt to explain how C# code works. That can be found in another tutorial on this site by clicking here.

I will give you a basic rundown on how things work though. The using statements are namespaces that point to classes to be used by the program. What is important to note however is the section that shows protected void Button1_Click(object sender, EventArgs e) because this area contains the code that is executed when the button is pressed.

Now look at the next line that shows Label2.Text = "Hello, World!". This line prints the text message Hello, World on top of Label1.

ASP.NET Textbox controls

c# programming language

In this next example I will be showing you how to read information from a textbox and print it to the screen. This program will continue to build on the first example adding new things as we progress. Now examine the line that shows protected void Button2_Click(object sender, EventArgs e). This line is activated when the second button is pressed. Then the program shows Response.Write("Are you enjoying this tutorial yet " + TextBox1.Text + "?"); on the next line.

The Response.Write is how ASP.NET writes to the screen. Notice that it printed at the top. That is because all of the other print messages were to either a label or a textbox. This time however the system printed directly to the screen so it will actually start on the first line. The next part inside the quotes is where you put your text message.

After that you will notice this part + TextBox1.Text + "?". This is extracting the message that is typed into the textbox and adding to the message in quotes. Another name for this technique is called concatenation. That means it is joining a series of strings together. There are three parts of the concatenation. The first part is the message inside the double quotes. The second part is the TextBox1.Text statement, which holds the message entered in the textbox. Finally the "?" adds another string to the sequence. The plus signs in between is what ties everything together.

ASP.NET Checkbox controls

programming c#

Now we will cover how Checkboxes work. First of all a checkbox allows you to make a selection from a list. When you click on a box it will be checked. Then ASP.NET can find out how many boxes you checked and then show a message. In this example there are 3 checkboxes called Surprise 1, Surprise 2, and Surprise 3.

When the first checkbox is clicked the line if (CheckBox1.Checked) prize += "50 dollars"; is read. Before understanding this line look above at this line string prize. That is necessary to initialize the variable as a string so that ASP.NET can use it.

Okay remember what you learned earlier about concatenation. The prize += is actually read as prize = prize + 1. So this is taking the first message in the variable prize and adding it to the second, third, etc. Then the next part "50 dollars" is the message that will print each time. I am showing this so you can understand that there is more than one way to append data together.

Since the middle checkbox was selected the message printed out 100 dollars (prize variable). Variables are a good way to keep track of changing data in your program.

XML code

c# .net training

Okay now before the program gets too large I figured it would be beneficial to explain another important part that ASP.NET keeps track of called the xml code. If you examine the picture you will notice that it looks very similar to html.

However the first line (known as declaration statement) will need some explanation. The Page Language="C#" is using Visual C#. The next line AutoEventWireup="false" is a property setting that automatically loads Page events and methods. Setting this to false prevents the page event from being called twice, which is why it is not a good idea to set it to true.

The next line shows CodeFile="Default.aspx.cs" which is a directive that points to the C# (C Sharp) file. This is the code that was shown earlier when you saw the Button examples.

Be sure to keep the next line Inherits="_Default" to the home page or Master page. This Inherits will allow Master Pages to carry over from one page to another much later. If you want to skip ahead to Master Pages, just click on the Videos tab at the top of my website page.

Subscribe to the my newsletter
Privacy Notice: Programmer Mind will not compromise your privacy