Professional Documents
Culture Documents
2008 SP1
Code-Behind Files
Using Code-Behind Files
Mixed code: Place the code in the same file as the content Inline code: Place the code in a separate section of the content file Code-behind file: Place the code in a separate file
The code-behind file is the default method for adding code in Visual Studio 2008 SP1
single file
Mixed code
Inline code
Single file
<content> <code>
Code-Behind Files
Code-behind files contain the programming logic for a single Web
Form
Separate files
<content>
<code>
Processed
Handlers Handlers
Events
Postback
Server-side process
Submit
URL Request
Value
Client
Server
Provide the code for the event handler in the codebehind file that handles the event
Types
Components
Classes
Types is a common name for value types or structures and reference types or objects
Components consist of one or more types that are compiled into a DLL assembly
Classes are reference types and are used as templates for objects
Handling Postbacks
Demonstration: How To Handle Page Events
the controls on updated the form (the list Page.IsPostBack in the Page_Load then sends the box and the button) run. In this event succeeds and in the information back to the the code client scenario, the box event the block runs. In list this example, handler changes the label code populates a list box. to
Brazil
Canada Italy
Canada Italy
Page1.aspx.vb Sub Page_Load() Sub If Page_Load() Not Page.IsPostBack If Not Then Page.IsPostBack ' Fill the list box Then ... ' Fill the list box ... End If End Sub
reflect the new list box election. The The server server then then returns returns the the page updated information to the client. to the user. In this example, the The user the same page, page has views a list box, a blank label, but the label has now changed to and a Submit button on it. reflect the list box selection. When If you the want the new value user changes theof the list box to be sent theand server selection in the listto box, immediately, and you do not then clicks the Submit button, want to wait for the user to click the information is sent back to the server. Submit button, you can set the the list box controls The server can determine that AutoPostBack property to True. this is a page that is being posted With the AutoPostBack property back to itself. Therefore, the test set to true, as soon as the user for Page.IsPostBack in the changes the selection in the list Page_Load event fails and box, the information is sentthe to
Submit
Listbox.SelectedIndex=1
Listbox.SelectedIndex=2
Client
Server
Handling Postbacks
Page_Load fires on every request: Use Page.IsPostBack to run conditional logic Page.IsPostBack prevents reloading for each postback
The following code samples show how the Page.IsPostBack property is used [Visual Basic] Protected Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then End If End Sub [Visual C#] protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { } }
Module Review
Review Questions Real-World Issues and Scenarios
Best Practices