Writing Your First ASP.NET Page


ASP.NET pages consist of code and markup and are dynamically compiled and executed on the server to produce a rendering to the requesting client browser. When a browser client requests .aspx resources, the ASP.NET runtime parses and compiles the target file into a .NET Framework class. This class can then be used to dynamically process incoming requests. The following example demonstrates a simple HTML page that collects a user’s name and category preference and then performs a form postback to the originating page when a button is clicked. It includes the following tools: Note that nothing happens yet when you click the Lookup button. This is because the .aspx file contains only static HTML (no dynamic content) or an empty event handler Lookup.Click( ). Thus, the same HTML is sent back to the client on each trip to the page, which results in a loss of the contents of the form fields (the text box and drop-down list) between requests.


intro1.aspx.cs (without using code render blocks)

using System;
namespace WebApplication2 {
  public partial class WebForm1 : System.Web.UI.Page {
    protected void Page_Load( object sender, EventArgs e ) {
    }
    protected void Lookup_Click( object sender, EventArgs e ) {
    }
  }
}

intro1.aspx (using code render blocks)

<%@ Page Language="C#" AutoEventWireup="true" 
   CodeBehind="intro2.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <body><center>
  <form ID="form1" runat="server">
   <h3>Name: <asp:TextBox ID="Name" runat="server">
    Category:
    <asp:DropDownList ID="Category" runat="server">
     <asp:ListItem Selected="True">psychology</asp:ListItem>
     <asp:ListItem>business</asp:ListItem>
     <asp:ListItem>popular_comp</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="Lookup" Text="Lookup"
        OnClick="Lookup_Click" runat="server" />
    <asp:Label ID="Result" runat="server" />
   </h3>
  </form>
 </center></body>
</html>

Web