You are on page 1of 27

Adding Code to a Microsoft

ASP.NET Web Form


Module 4: Adding Code to a Microsoft ASP.NET Web
Form

• Implementing Code-Behind Pages

• Adding Event Procedures to Web Server Controls

• Handling Page Events


Lesson: Implementing Code-Behind Pages
• Methods for Implementing Code

• Writing Inline Code

• What Are Code-Behind Pages?

• How Code-Behind Pages Work


Methods for Implementing Code
• Three methods for adding code:
 Put code in the same file as content (mixed)
 Put code in a separate section of the content file (inline code)
 Put code in a separate file (code-behind pages)

• Code-behind pages are the Visual Studio 2008 default


Writing Inline Code

• Code and content in the same file

• Different sections in the file for code and HTML


<HTML>
<asp:Button id="btn" runat="server"/>
</HTML>
<SCRIPT Language="vb" runat="server">
Sub btn_Click(s As Object, e As EventArgs) Handles
btn.Click
...
End Sub
</SCRIPT>

<HTML>
<asp:Button id="btn" runat="server"/>
</HTML>
<SCRIPT Language="c#" runat="server">
private void btn_Click(object sender, System.EventArgs
e)
{
. . .
}
</SCRIPT>
What Are Code-Behind Pages?
• Separation of code from content
 Developers and UI designers can work independently

Single file Separate files

code
<tags> code
<tags>

Form1.aspx Form1.aspx Form1.aspx.vb


or Form1.aspx.cs
How Code-Behind Pages Work
• Create separate files for user interface and interface
logic
• Use @ Page directive to link the two files

• Pre-compile or JIT-compile

Page1.aspx.cs
Page1.aspx
<% @ Page Language="c#" public class WebForm1
{
Inherits="Project.WebForm1" private void cmd1_Click()
Codebehind="Page1.aspx.cs" {
Src = "Page1.aspx.cs" %> …
}
}
Lesson: Adding Event Procedures to Web Server
Controls
• What Are Event Procedures?

• Demonstration: Using Events

• Client-Side Event Procedures

• Server-Side Event Procedures

• Multimedia: Client-Side and Server-Side Events

• Creating Event Procedures

• Demonstration: Creating an Event Procedure

• Interacting with Controls in Event Procedures


What Are Event Procedures?
• Action in response to a user’s interaction with the controls
on the page
Client-Side Event Procedures

• Typically, used only with HTML controls


• Interpreted by the browser and run on the
client
• Does not have access to server resources
• Uses <SCRIPT language="language">

Internet .HTM
Pages
Server-Side Event Procedures

• Used with both Web and HTML server


controls
• Code is compiled and run on the server
• Have access to server resources
• Use <SCRIPT language="vb"
runat="server"> or <SCRIPT language=“cs"
runat="server">

.ASPX
Internet
Pages
Multimedia: Client-Side and Server-Side Events
Creating Event Procedures

• Visual Studio .NET declares variables and creates an


event procedure template

Private Sub cmd1_Click(ByVal s As System.Object, _


ByVal e As System.EventArgs) Handles cmd1.Click

End Sub

private void cmd1_Click(object s, System.EventArgs e)


{

• Using the Handles keyword adds many event


procedures to one event
Demonstration: Creating an Event Procedure
• Create a Web Form using Visual Studio .NET

• Add controls to the Web Form

• Double-click one or more controls to add event procedures

• Build and Browse


Interacting with Controls in Event Procedures

• Read the properties of Web server controls

strGreeting = "Hello " & txtName.Text

strGreeting = "Hello " + txtName.Text;

• Output responses to other Web server controls

lblGreeting.Text = "new text"

lblGreeting.Text = "new text“;


Lesson: Handling Page Events
• The Page Event Life Cycle

• Multimedia: The PostBack Process

• Demonstration: Handling Events

• Handling Page.IsPostback Events

• Linking Two Controls Together

• Demonstration: Linking Controls Together


The Page Event Life Cycle

Page_PreInit
Page_PreInit
Page_Init
Page_Init
Page_PreLoad
Page_PreLoad
Control events Page_Load
Page_Load
Change Events Textbox1_Changed
Textbox1_Changed
Action Events Button1_Click
Button1_Click
Page_Unload
Page_Unload

Page is disposed
Multimedia: The PostBack Process
Demonstration: Handling Events
Handling Page.IsPostback Events

• Page_Load fires on every request


 Use Page.IsPostBack to execute conditional logic
 Page.IsPostBack prevents reloading for each postback
Linking Two Controls Together

• Linking one control to another is useful for taking


values from list boxes or drop-down lists
• Data binding
Demonstration: Linking Controls Together
• Link a Label to a ListBox
Adding Functionality to a Web Application
• Exercise 1: Creating a Page_Load Event Procedure

• Exercise 2: Creating a Click Event Procedure

• Exercise 3: (If Time Permits): Implementing a Component


in a User Control

Logon information
Virtual machine 2310C_04
User name Student
Password Pa$$w0rd

Estimated time: xx minutes


Lab Scenario

Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config

Life Insurance Retirement Medical Dental


Life.aspx Retirement.aspx Medical.aspx Dental.aspx

Prospectus Doctors User Control XML Web


Lab Web Prospectus.aspx Doctors.aspx namedate.ascx Service
Application dentalService1.asmx

XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• What is the advantage of using code-behind pages when
adding functionality to a Web Form?
• How is an event procedure associated with an event of a
server control?
• How is a code-behind page associated with an .aspx page?

• Why would you want to set up your code-behind pages to


be precompiled instead of just-in-time?
• When does a form post back to itself?
Review for Alpha
• Is there any topic or specific content item in the module
that seemed unclear or unnecessary?
• Is there any content item/related subject area that was
not covered and could be included?
• Did you observe any issues with the technical accuracy of
the content?
• Is the content in the module presented in a manner that
encourages learning? Did the flow of topics seem right?
• Does the lab outline indicate the expected scope of tasks
to be covered? Would you like to suggest any tasks that
could be removed or added?

You might also like