Check whether the user has logged in on the master page, so you don't have to check on every page. Write the following code in its Init event:
protected void ContentPlaceHolder1_Init(object sender, EventArgs e)
{
if (Session["loginid"] == null || Session["role"]==null)
{
Response.Redirect("../Main.aspx");
}
}
Attached:
If a Page uses a MasterPage, the execution sequence of events between the two is as follows:
MasterPage control Init event.
Page control Init event.
MasterPage Init event.
PageInit event.
Page Load event.
MasterPage Load event.
Page control Load event.
Page PreRender event.
MasterPage PreRender event.
MasterPage control PreRender event.
Page control PreRender event.
The above content can be found on MSDN. One additional point: The execution order of the New function is Page.New() --> MasterPage.New()
http://www.cnblogs.com/floatping/archive/2007/01/03/610760.html