Eventos de P�gina
As� como los controles de servidor pueden tener eventos, tambi�n las p�ginas disparan eventos. El m�s habitual de los eventos es el que se produce cuando la p�gina se carga, es el evento "Load". Este evento es disparado siempre que se carga la p�gina.
En el siguiente ejemplo tenemos una etiqueta que se actualiza cada vez que se carga la p�gina con la hora actual del servidor.
Ejemplo02.aspx
<?@ Page language="c#" Codebehind="Ejemplo02.aspx.cs" AutoEventWireup="false" Inherits="WebEstiloEjemplos.ejem02.Ejemplo02" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Ejemplo02</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Ejemplo02" method="post" runat="server">
<h1>WebEstilo - Ejemplo 02</h1>
<asp:Label id="lblHora" runat="server"></asp:Label>
</form>
</body>
</HTML>
El correspondiente c�digo C# asociado a la p�gina.
Ejemplo02.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebEstiloEjemplos.ejem02
{
/// <summary>
/// WebEstilo.com
/// Clase que muestra el funcinamiento del evento Load.
/// </summary>
public class Ejemplo02 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblHora;
private void Page_Load(object sender, System.EventArgs e)
{
// C�digo que se ejecuta cada vez que se carga la p�gina
lblHora.Text = "Hora actual: "+ DateTime.Now.ToShortTimeString();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}