ASP.NET Forms
Learn to write code in Web Forms: one button, one label, and a small family of classes that answer the same question differently.
Sklyarenko Vlad
Develop an application using a Web Form
The goal of the work is to learn to write code in Web Forms. These are the steps from the lecture. Tick them off as you go.
0 of 7 done
On this site the pair is named project-4-form.aspx and project-4-form.aspx.cs instead of Default.aspx, so it can sit next to the other projects.
Press Start and watch the code run
This is a browser copy of the Web Form with the same logic. Pick which class goes into each seat of the family array, press Start, and follow the call from the button down to the label.
Project 4. ASP.NET OOP
One parent, two children
APerson says a method can be changed (virtual). AMan and AWoman inherit from it (: APerson) and change it (override). The array is typed APerson[], yet each call runs the version of the real object. That is polymorphism. Change the selects above to see it.
The ASP.NET page
Here is the markup and the code-behind. The label and the button are server controls (runat="server"), and OnClick connects the button to the C# method.
From a console program to a Web Form
The lecture shows the same program as a console application. Almost nothing changes, only the entry and the output.
| Console program | Web Form | |
|---|---|---|
| Where it starts | Main() | cmbStart_Click(), after a click |
| Methods | static | internal, of the page class |
| Output | Console.Write(result) | lblResult.Text = result |
| New line | "\n\r" | "<br>" |
Open the real page
This button opens project-4-form.aspx. It is processed by the server, so it needs both files on an ASP.NET host. On a plain file server, or when you open the files from your computer, it will not run.
Names and ideas
Hungarian notation
A short prefix tells you the kind of control, so cmbStart is obviously the Start button.
| Prefix | Control | Example |
|---|---|---|
| lbl | Label | lblResult |
| cmb | Button (the name used in the lecture) | cmbStart |
| txt | TextBox | txtName |
| chk | CheckBox | chkAgree |
| ddl | DropDownList | ddlCity |
- Model, View, ViewModel
The Model builds the answer, the View shows it, and the ViewModel calls both in order.
- Round trip
A click sends the page to the server. The C# code runs there, and the server sends back a new page.
- Partial class
The page class is split in two files: the markup (.aspx) and your code (.aspx.cs).