Aspnet Events
# ASP.NET Web Forms - Event Handling
An event handler is a specialized subroutine (or method) designed to execute specific code in response to a given event, such as a page loading or a user clicking a button.
---
## Understanding ASP.NET Event Handlers
Consider the following classic ASP-style code snippet:
```html
<%
lbl1.Text = "The date and time is " & Now()
%>
```
When exactly will the code inside the `<% ... %>` block be executed? The answer is unpredictable and lacks structure. In modern web development, we need precise control over execution timing. This is where event-driven programming in ASP.NET Web Forms comes in.
---
## The `Page_Load` Event
The `Page_Load` event is one of the most fundamental lifecycle events in ASP.NET. It is triggered automatically every time a page is loaded. ASP.NET detects this event and executes the code inside the corresponding `Page_Load` subroutine.
### Example: Basic Page_Load Event
```html
```
> **Note:** Unlike control-specific event handlers, the default `Page_Load` event handler in this simplified syntax does not strictly require object references (`sender`) or event arguments (`e`).
---
## The `Page.IsPostBack` Property
By default, the `Page_Load` subroutine runs **every single time** the page is requestedβboth on the initial load and on subsequent postbacks (such as when a user clicks a button that submits the form back to the server).
To execute code **only** when the page is loaded for the first time, you can use the `Page.IsPostBack` property:
* **`Page.IsPostBack = False`**: The page is being loaded for the very first time (Initial Request).
* **`Page.IsPostBack = True`**: The page is being posted back to the server (e.g., via a button click or form submission).
### Example: Using IsPostBack to Prevent Re-initialization
```html
```
### How It Works:
1. **First Visit:** When you first navigate to the page, `Page.IsPostBack` is `False`. The code inside the `If` block runs, displaying the current date and time in `lbl1`.
2. **Button Click (Postback):** When you click the "Submit" button, the page posts back to the server.
* `Page.IsPostBack` is now `True`, so the code inside the `Page_Load` `If` block is skipped, preserving the original timestamp in `lbl1`.
* The `Submit` event handler is triggered, which updates `lbl2` to display "Hello World!".
---
## Key Considerations for ASP.NET Events
* **Page Lifecycle Order:** Events in ASP.NET Web Forms execute in a strict sequence (e.g., `Page_Init` -> `Page_Load` -> Control Events like `Button_Click` -> `Page_PreRender` -> `Page_Unload`). Understanding this sequence is crucial for managing state and control data.
* **State Preservation:** ASP.NET uses `ViewState` to preserve control values across postbacks. Combining `ViewState` with `Page.IsPostBack` checks prevents unnecessary database queries and performance bottlenecks during postbacks.
* **Event Arguments:** Standard control event handlers (like `Submit` in the example above) require two parameters:
* `sender As Object`: Represents the control that triggered the event.
* `e As EventArgs`: Contains event-specific data.
YouTip