-
Rather than providing a standalone program,
we could instead provide a form in HTML
which causes a program to be run
(when the form's
button is clicked).
-
Traditionally, this program is a CGI program often written in Perl.
-
Or, if you have appropriate web server software (such as Apache's httpd),
it could be written in PHP.
-
Or, if you use tomcat,
it could be written in Java (a servlet).
-
And now,
if you use IIS (Microsoft's web server software),
the program could be written in a .NET language.
-
Such a WWW page is called an ASP.NET page.
-
This HTML includes a directive to IIS to say that the
supporting code is in C# and it is in the file
WebForm1.aspx.cs.
-
The directive also refers to the class
WebFormConvert.WebForm1.
-
And the ids
TextBox1,
Button1
and
Label1
are references to fields of the class.
-
As well as producing the webform,
Visual Studio.NET automatically generates a lot of code in
C#
(or VB.NET).
-
This code includes declarations for
TextBox1,
Button1
and
Label1.
-
The only code you need to provide yourself is the body of the
Button1_Click method.
-
This method gets executed
whenever the user hits the
Get Fahrenheit
button of the form.
-
When
Button1_Click
refers to
TextBox1.Text,
it is referring to whatever text the user typed into the
text box of the form.
-
Similarly, when
Button1_Click
assigns a value to
Label1.Text,
it is changing what appears in the label of the form.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebFormConvert
{
public class WebForm1 : Page
{
protected TextBox TextBox1;
protected Button Button1;
protected Label Label1;
private void Page_Load(object sender, EventArgs e)
{
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, EventArgs e)
{
double tCentigrade = double.Parse(TextBox1.Text);
double tFahrenheit = 32 + tCentigrade*9/5;
Label1.Text = tFahrenheit.ToString();
}
}
}