title: Accessing variable from code behind Master Page
body: <p><strong>Please see the image below</strong></p>
  
  <p><img src="https://i.stack.imgur.com/l2ZGF.png" alt="enter image description here"></p>
  
  <p>As you can see I have a master page <code>Design.master</code> and the code behind it that is called <code>Design.master.cs</code>.</p>
  
  <p>I am simply trying to get my page to redirect to <code>dashboard.aspx</code> when the user is not logged it, but no matter what I try I can't get it do it.</p>
  
  <p>I want to be able to access the value of the public variable <code>redirect</code>, which is set in the code behind and access it from the <code>Design.master</code> code.</p>
  
  <p>So that I can check the variable and put the <code>Response.redirect()</code> code in the <code>Design.master</code> page.</p>
  
  <p>Unless you know how to get the Redirect to work from the <code>Design.master.cs</code> code?</p>
  
  <p>Please help :( It's driving me INSANE!</p>
  
  <p><strong>Updated image</strong></p>
  
  <p><img src="https://i.stack.imgur.com/LaN3G.png" alt="adas"></p>
  
  <p><code>Design.master</code> code:</p>
  
  <pre><code>&lt;%@ Master Language="C#" CodeBehind="~/Design.master.cs" AutoEventWireup="true" %&gt;
  
  &lt;%
  
          Response.Redirect("default.aspx", true);
  
  %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;title&gt;Portal&lt;/title&gt;
      &lt;link rel="stylesheet" href="styles.css" type="text/css" /&gt;
  
      &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="portal_script.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div id="main_wrapper"&gt;
  
          &lt;div id="header_top_wrapper"&gt;
              &lt;div id="header_top"&gt;
                  &lt;img src="images/design/logo_small.png" id="logo_top" /&gt;
  
                  &lt;a href="basket.aspx"&gt;
                      &lt;div id="basket_top_link"&gt;
                          &lt;div&gt;Order Basket&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div id="header_breadcrumb_wrapper"&gt;
              &lt;div id="header_breadcrumb"&gt;&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div id="content_wrapper"&gt;
              &lt;div id="content"&gt;
  
                  &lt;div id="side_menu"&gt;
  
                      &lt;div id="customer_info"&gt;
  
                          &lt;asp:ContentPlaceHolder id="CustomerInfo" runat="server"&gt;
  
                          &lt;/asp:ContentPlaceHolder&gt;
  
                      &lt;/div&gt;
  
                      &lt;a href="dashboard.aspx"&gt;&lt;div class="side_menu_item"&gt;
                          &lt;div class="item_title"&gt;View Orders&lt;/div&gt;
                          &lt;div class="item_description"&gt;View your outstanding orders&lt;/div&gt;
                      &lt;/div&gt;&lt;/a&gt;
  
                      &lt;a href="search.aspx"&gt;&lt;div class="side_menu_item"&gt;
                          &lt;div class="item_title"&gt;Product Search&lt;/div&gt;
                          &lt;div class="item_description"&gt;Search products and view stock levels&lt;/div&gt;
                      &lt;/div&gt;&lt;/a&gt;
  
                      &lt;a href="search.aspx"&gt;&lt;div class="side_menu_item"&gt;
                          &lt;div class="item_title"&gt;Outstanding Complaints&lt;/div&gt;
                          &lt;div class="item_description"&gt;View status of complaints&lt;/div&gt;
                      &lt;/div&gt;&lt;/a&gt;
  
                  &lt;/div&gt;
  
                  &lt;div id="content_left"&gt;
  
                      &lt;asp:ContentPlaceHolder id="Content" runat="server"&gt;
  
                      &lt;/asp:ContentPlaceHolder&gt;
  
                  &lt;/div&gt;
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div id="footer_wrapper"&gt;
  
              &lt;div id="footer"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;
  
          &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Design.master.cs code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Design : System.Web.UI.MasterPage
  {
  
      public bool redirect = false;
  
      protected override void OnInit(EventArgs e)
      {
  
          // Get the users session
          SessionManager session = new SessionManager(HttpContext.Current);
  
          // I've tried it here, and it won't redirect
          // Response.Redirect("default.aspx");
  
          redirect = true;
  
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          // I've tried it here, and it won't redirect
          // Response.Redirect("default.aspx");
      }
  }
  </code></pre>