8
Christopher M. Pascucci .NET Programming: WebForm Events

Christopher M. Pascucci.NET Programming: WebForm Events

Embed Size (px)

Citation preview

Page 1: Christopher M. Pascucci.NET Programming: WebForm Events

Christopher M. Pascucci

.NET Programming: WebForm Events

.NET Programming: WebForm Events

Page 2: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events Events occur as a response to a user’s actions that triggers a response

from the server. Click of a button Selection of an item from a dropdown list Pressing a key Moving the mouse

There are Page level events that are already defined and can be used by the programmer. Page_Load Page_UnLoad Page_Init

Page 3: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events Functions that respond to an event can be located in the following

places: If the function is to be processed on the client-side, then it must be in

coded and located in between a script tag on the aspx page,

without the runat=“server”.

<script> </script>

If the function is to be processed on the server-side within the aspx page, then it must be in coded in between a script tag on the aspx page,

with the runat=“server”.

<script runat=“server”> </script>

If the function is to be processed on the server-side in a CodeBehind, then is must be coded in the CodeBehind file with the following syntax :

Public Sub SomeName(ByVal sender As Object, ByVal e As EventArgs)

Page 4: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events Note: A more convenient way to create a runat server event Sub in

the CodeBehind is simply to double click on the control on the ASPX page in Design Mode. This will automatically create the proper, but slightly different, Sub syntax in the

Code Behind (and place you in the Code Behind page as well).

Private Sub btnMySubmit_Click(ByVal source As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click

Page 5: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events Examples:

1.) The OnClick event transfers control to the function cmdProcess() that is processed on the client side.

<INPUT type="button" name="btnNew" OnClick="cmdProcess()" >

2.) The OnClick event transfers control to the function cmdProcess() that is processed on the server side.

<INPUT type="button" name="btnNew" OnServerClick="cmdProcess()" runat="server" >

3.) Processed on server side by a public function named cmdProcess() without handler

<asp:button id="btnUpdate” OnClick="cmdProcess()" runat="server"> </asp:button>

4.) Processed on server side by the private function implemented by double-clicking on the dropdown list in the Design mode.

<asp:button id="btnUpdate” runat="server"> </asp:button>

Page 6: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events Examples:

5.) Processed on server side by a public function named listProcess without handler

<asp:dropdownlist id="DropDownList1“ OnSelectedIndexChanged="listProcess" runat="server" AutoPostBack="True"></asp:dropdownlist>

6.) Processed on server side by the private function implemented by double-clicking on the dropdown list in the Design mode.

<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True"></asp:dropdownlist>

Written in the CodeBehind automatically

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged.

Page 7: Christopher M. Pascucci.NET Programming: WebForm Events

Web Forms Events There are many other events for WFC and HFC controls other than the

onclick. When using the VS.NET IDE you can find all events that a particular control can respond to by simply typing a space after the control tag. These events have a yellow lightning icon when using intellisense.

HFC: onkeyup, onkeydown, onkeypress onchange (textbox or dropdownlist)

WFC: OnTextChanged, OnSelectedIndexChange

You must set the attribute AutoPostback=“True” or it will do nothing.

When using these events, AutoPostback=True causes a Postback to occur that will transfer control back to the server.

Page 8: Christopher M. Pascucci.NET Programming: WebForm Events

Program Flow Control When the ASPX page is loaded, there is a ten step lifecycle that is fairly complex. A

description of this lifecycle is presented in the textbook on page 193. Another description and web article Reference are also provided at this link.

However, for the purpose of this course, a highly simplified version of these ten steps is presented here as a 3 step lifecycle:1. If there is a PageLoad() function in the CodeBehind, it will execute before any other processing

on the page. You can use IsPostback to determine whether it is the initial load of the page or any subsequent load.

2. If there is an event raised from the aspx form (as discussed in previous slides) on the client side, and there is a Code Behind function that responds to this event, then it will execute. It is important to note that if you use both the PageLoad() function and an event responding function in the CodeBehind, then both will run, but PageLoad() runs first.

3. All server side code in the aspx page will be execute. This includes <script runat=server></script> and <% … %>

When the CodeBehind and the aspx script have been completely processed (and its html downloaded to the browser), the CodeBehind object is destroyed. If there is a postback, the aspx page and its CodeBehind are reloaded, and the CodeBehind object is instantiated again. This means that all objects declared within the CodeBehind are also re-instantiated.