title: Check Session variable and Redirect to login page before page load
body: <p>How can I check a variable and redirect to another page before the page loads using ASP.NET?</p>
  
  <p>I'm aware of the life cycle, and <code>PageInit()</code> sounds like it would be right, but I can't seem to find anywhere to put the code without and error within Visual Studio.</p>
  
  <p>I can't put the <code>onpageinit=""</code> within the first line of my page declaration.  Am I suppose to put it somewhere different?  My page declaration looks like:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="dashboard.aspx.cs" Inherits="dashboard" MasterPageFile="~/Design.master" %&gt;
  </code></pre>
  
  <p>This is the code that I want to run on the page load:</p>
  
  <pre><code>    // Check if the user is logged in, if not send them to the login page
      if (session.logged_in == false)
      {
          // Redirect the user to the login page
          Response.Redirect("login.aspx");
  
      }
  </code></pre>