[ Home | Syllabus | Course Notes | Assignments | Search]
Create a CS file containing event handlers, helper methods, and other code—everything that would normally appear between <script> and </script> tags in an ASPX file. Make each of these source code elements members of a class derived from System.Web.UI.Page.
In your Page-derived class, declare protected fields whose names mirror the IDs of the controls declared in the ASPX file. For example, if the Web form includes a pair of TextBox controls whose IDs are UserName and Password, include the following statements in your class declaration:
protected TextBox UserName; protected TextBox Password;
Without these fields, the CS file
won’t compile because references to UserName
and Password are unresolvable. At
run time, ASP.NET maps these fields to the controls of the same name
so that reading UserName.Text, for
example, reads the text typed into the TextBox
control named UserName.
Code Behind class is the base class of the code generated by the .aspx
page
Compile the CS file into a DLL and place the DLL in a subdirectory named bin in the virtual directory that holds the ASPX file.
Place the HTML portion of the Web form—everything between the <html> and </html> tags—in an ASPX file. Include in the ASPX file an @ Page directive containing an Inherits attribute that identifies the Page-derived class in the DLL.
<%@ Page Inherits="LanderPage" %> <html> <body> <h1>Lunar Lander</h1> <form runat="server"> <hr> <table cellpadding="8"> <tr> <td>Altitude (m):</td> <td><asp:Label ID="Altitude" Text="15200.0" RunAt="Server" /></td> </tr> <tr> <td>Velocity (m/sec):</td> <td><asp:Label ID="Velocity" Text="0.0" RunAt="Server" /></td> </tr> <tr> <td>Acceleration (m/sec2):</td> <td><asp:Label ID="Acceleration" Text="-1.6" RunAt="Server" /></td> </tr> <tr> <td>Fuel (kg):</td> <td><asp:Label ID="Fuel" Text="8165.0" RunAt="Server" /></td> </tr> <tr> <td>Elapsed Time (sec):</td> <td><asp:Label ID="ElapsedTime" Text="0.0" RunAt="Server" /></td> </tr> <tr> <td>Throttle (%):</td> <td><asp:TextBox ID="Throttle" RunAt="Server" /></td> </tr> <tr> <td>Burn Time (sec):</td> <td><asp:TextBox ID="Seconds" RunAt="Server" /></td> </tr> </table> <br> <asp:Button Text="Calculate" OnClick="OnCalculate" RunAt="Server" /> <br><br> <hr> <h3><asp:Label ID="Output" RunAt="Server" /></h3> </form> </body> </html>
using
F1: "Page" directive uses the "inherits" attribute to indicate there is code behind this page
F2: HTML Tag which creates a "Table" with rows "<tr>" and Data items "<td>"
using System; using System.Web.UI; using System.Web.UI.WebControls; public class LanderPage : Page { const double gravity = 1.625; // Lunar gravity const double landermass = 17198.0; // Lander mass protected Label Altitude; protected Label Velocity; protected Label Acceleration; protected Label Fuel; protected Label ElapsedTime; protected Label Output; protected TextBox Throttle; protected TextBox Seconds; public void OnCalculate (Object sender, EventArgs e) { double alt1 = Convert.ToDouble (Altitude.Text); if (alt1 > 0.0) { // Check for blank input fields if (Throttle.Text.Length == 0) { Output.Text = "Error: Required field missing"; return; } if (Seconds.Text.Length == 0) { Output.Text = "Error: Required field missing"; return; } // Extract and validate user input double throttle; double sec; try { throttle = Convert.ToDouble (Throttle.Text); sec = Convert.ToDouble (Seconds.Text); } catch (FormatException) { Output.Text = "Error: Invalid input"; return; } if (throttle < 0.0 || throttle > 100.0) { Output.Text = "Error: Invalid throttle value"; return; } if (sec <= 0.0) { Output.Text = "Error: Invalid burn time"; return; } // Extract flight parameters from the Label controls double vel1 = Convert.ToDouble (Velocity.Text); double fuel1 = Convert.ToDouble (Fuel.Text); double time1 = Convert.ToDouble (ElapsedTime.Text); // Compute thrust and remaining fuel double thrust = throttle * 1200.0; double fuel = (thrust * sec) / 2600.0; double fuel2 = fuel1 - fuel; // Make sure there's enough fuel if (fuel2 < 0.0) { Output.Text = "Error: Insufficient fuel"; return; } // Compute new flight parameters Output.Text = ""; double avgmass = landermass + ((fuel1 + fuel2) / 2.0); double force = thrust - (avgmass * gravity); double acc = force / avgmass; double vel2 = vel1 + (acc * sec); double avgvel = (vel1 + vel2) / 2.0; double alt2 = alt1 + (avgvel * sec); double time2 = time1 + sec; // If altitude <= 0, then we've landed if (alt2 <= 0.0) { double mul = alt1 / (alt1 - alt2); vel2 = vel1 - ((vel1 - vel2) * mul); alt2 = 0.0; fuel2 = fuel1 - ((fuel1 - fuel2) * mul); time2 = time1 - ((time1 - time2) * mul); if (vel2 >= -4.0) Output.Text = "The Eagle has landed"; else Output.Text = "Kaboom!"; } // Update the Labels to show latest flight parameters Altitude.Text = alt2.ToString ("f1"); Velocity.Text = vel2.ToString ("f1"); Acceleration.Text = acc.ToString ("f1"); Fuel.Text = fuel2.ToString ("f1"); ElapsedTime.Text = time2.ToString ("f1"); } } }
F3: set of controls and their behavior is different between Forms and Web Forms
F4: This is how the code behind names the ASP objects in the HTML page - so variable name and HTML tag "ID" attribute MUST agree (see HTML form for correspondence.
F5: ToString optionally takes a "formatString" parameter "f1" means floating point with one decimal place