Professional Documents
Culture Documents
Applications Using
Microsoft® Visual
Studio® 2008
Module 4: Adding Code to a Microsoft ASP.NET
Web Form
• Implementing Code-Behind Pages
[Visual C#]
<SCRIPT Language="c#" runat="server">
private void Button1_Click(object sender,
System.EventArgs e) {
}
</SCRIPT>
<html xmlns="http://www.w3.org/1999/xhtml">
<asp:Button ID=“Btn1" runat="server" Text=“Button" />
</html>
[Visual Basic]
<SCRIPT Language="vb" runat="server">
Sub Button1_Click(ByVal s As Object, ByVal e As _
EventArgs) Handles Button1.Click
End Sub
</SCRIPT>
<html xmlns="http://www.w3.org/1999/xhtml">
<asp:Button ID="Btn1" runat="server" Text="Button" />
</html>
What Are Code-Behind Pages?
• Separation of code from content
Developers and UI designers can work independently
code
<tags> code
<tags>
• Pre-compile or JIT-compile
Default.aspx Default.aspx.cs
<%@ Page Language="C#" public partial class _Default :
AutoEventWireup="true“ System.Web.UI.Page
CodeFile="Default.aspx.cs {
" Inherits="_Default" %> private void Button1_Click()
{
...
}
}
Lesson: Adding Event Procedures to Web
Server Controls
• What Are Event Procedures?
Client-side
Internet
Internet
.HTM
Pages
Server-Side Event Procedures
• Implement for both Web and HTML server
controls
• Compiled and run on the server
Server-side
Internet
Internet
Internet .ASPX
Pages
Multimedia: Client-Side and Server-Side Events
Creating Event Procedures
• Visual Studio 2008 creates an event procedure
template
[Visual C#]
private void Button1_Click(object s, System.EventArgs e)
{
[Visual Basic]
Private Sub Button1_Click(ByVal s As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub
[Visual C#]
greetingString = "Hello " & nameTextBox.Text;
[Visual Basic]
greetingString = "Hello " + nameTextBox.Text;
[Visual C#]
greetingLabel.Text = "new text";
[Visual Basic]
greetingLabel.Text = "new text"
Lesson: Handling Page Events
• The Page Event Life Cycle
Page_PreInit
Page_Init
Page_PreLoad
Page_Unload
Page is disposed
Multimedia: The PostBack Process
Demonstration: Handling Events
• View the page
[Visual C#]
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
}
}
[Visual Basic]
Protected Sub Page_Load(ByVal s As System.Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
End If
End Sub
Linking Two Controls Together
<form runat="server">
[Visual C#]
<asp:DropDownList id=“occupationList"
protected void Page_Load(object sender, EventArgs e)
{ autoPostBack="true" runat="server" >
<asp:ListItem>Program Manager</asp:ListItem>
selectedValueLabel.DataBind();
} <asp:ListItem>Tester</asp:ListItem>
<asp:ListItem>User Assistance</asp:ListItem>
</asp:DropDownList>
[Visual
<p>YouBasic]
selected: <asp:Label id=“selectedValueLabel"
Protected Sub Page_Load(ByVal
Text="<%# sender As Object, _%>"
occupationList.SelectedItem.Text
ByVal e As System.EventArgs)
runat="server" /> Handles Me.Load
selectedValueLabel.DataBind()
</p>
End Sub
</form>
Demonstration: Linking Controls Together
• View the controls
Logon information
Prospectus
prospectus.aspx Doctors User Control XML Web
LINQ to SQL doctors.aspx nameDate.ascx Service
Classes DentalService1.asmx
Doctors.dbml
Doctors Dentists
XML Files
Lab Review
Review Questions
• How can you run code only when a Web page loads for the
first time?
• Where do the control event procedures occur in the page
event life cycle?
• What is the default event procedure for common controls?
• Best Practices