您的位置:首页 > 其它

page_load 与page_init 的区别

2008-09-30 16:22 337 查看
page_load 与page_init 的区别

默认新建一个页面,就会有一个Page_Load事件的默认代码,而Page_Init却是另一个对页面作用非常重要的事件。

下面分别说明:

Page_Init:是在页面未加载之前,也就是在页面初始化之前,在Page_Load之前调用的,可以在控件加载之前做一些客户端检测呀这些操作等。但这个事件对于当前用户来说,只会进行一次,也就是第一次访问这个页面的时候,其运行一次。

Page_Load:在Page_Init之后运行(废话),用于加载控制以及页面的其它内容。客户端每刷新或是提交一次,Page_Load事件就重新绘制页面,将当前页面当新页面来处理。

区别说清楚了,再来说一下这点区别的利用。

我想到的是,可以利用这一点来做页面点击或是访问次数的更加精确一点的计数。

比如说,在Page_Init里运行计数的代码,这样这个页面被同一个用户不停的刷或是操作的时候,其点击始终是1次,这样计数就比较科学一些。当然,不能防止去了别的页面再回来的情况了。

还有比如我以前网站的访问统计,记录IP访问以及网站总的页面访问次数,都可以利用。

这里只是一个小小的想法,具体实现请自己DIY。。

例子:

以下代码共二个文件,保存之后,运行即可

文件名(1):calendar.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class control_calendar : System.Web.UI.Page

{

protected void Page_Init(object sender, EventArgs e) //如果此处为page_load,那么,先运行一下,点击下拉的年,月,日,然后点击“提交”按钮后,再来选择下拉的年月日的时候,注意观察,会发现出现的重复的下拉数据;但是用Page_init就不会有此现象发生!

{

for (int y = 1980; y < 2050; y++)

{

ye.Items.Add(y.ToString());

}

for (int m = 1; m < 13; m++)

{

mo.Items.Add(m.ToString());

}

for (int d = 1; d < 31; d++)

{

da.Items.Add(d.ToString());

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

switch (DropDownList1.SelectedValue)

{

case "None":

Calendar1.SelectionMode = CalendarSelectionMode.None;

break;

case "Day":

Calendar1.SelectionMode = CalendarSelectionMode.Day;

break;

case "DayWeek":

Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;

break;

case "DayWeekMonth":

Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;

break;

}

}

protected void Calendar1_SelectionChanged(object sender, EventArgs e)

{

Label1.Text = Calendar1.SelectedDate.ToShortDateString();

Label2.Text = Calendar1.SelectedDate.Day.ToString()+"号";

Label3.Text = Calendar1.SelectedDate.Month.ToString() + "月";

Label4.Text = Calendar1.SelectedDate.Year.ToString() + "年";

}

}

文件名(2).calendar.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="calendar.aspx.cs" Inherits="control_calendar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>无标题页</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

<asp:ListItem Value="None">不选择</asp:ListItem>

<asp:ListItem Value="Day">天</asp:ListItem>

<asp:ListItem Value="DayWeek">天、周</asp:ListItem>

<asp:ListItem Value="DayWeekMonth">天、周、月</asp:ListItem>

</asp:DropDownList>

<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"

BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"

ForeColor="#663399" Height="200px" SelectionMode="None" ShowGridLines="True" Width="220px" OnSelectionChanged="Calendar1_SelectionChanged">

<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />

<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />

<SelectorStyle BackColor="#FFCC66" />

<OtherMonthDayStyle ForeColor="#CC9966" />

<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />

<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />

<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />

<DayStyle BorderColor="PeachPuff" />

</asp:Calendar>

<br />

当前选中的日期是:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />

当前选中的天是:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />

当前选中的月是:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />

当前选中的年是:

<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />

<br />

<asp:DropDownList ID="ye" runat="server">

</asp:DropDownList>年<asp:DropDownList ID="mo" runat="server">

</asp:DropDownList>月<asp:DropDownList ID="da" runat="server">

</asp:DropDownList>日 

<asp:Button ID="Button1" runat="server" Text="设置" /></div>

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: