title: Enabling viewstate on dropdown when page viewstate is off
body: <p>I have a .Net 3.5 web application that has view state disabled on the page level but enabled on the dropdown control. During post back of the dropdown I am getting the following exception</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>This exception happens on following line:</p>
  
  <pre><code>string selectedVal = ddlStatus.SelectedItem.Value;
  </code></pre>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstatemode.aspx" rel="nofollow noreferrer">Control.ViewStateMode Property</a> is from <code>.Net 4.0</code> onwards. It meets the purpose if I were using <code>4.0</code></p>
  
  <blockquote>
    <p>To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.</p>
  </blockquote>
  
  <p>How can we resolve the problem when I am using .Net 3.5 framework?</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>public partial class FormattingTest : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          Status status1 = new Status() { StatusCode = 1, StatusDescription = "Inctive" };
          Status status2 = new Status() { StatusCode = 2, StatusDescription = "Active" };
          List&lt;Status&gt; statucCollection = new List&lt;Status&gt;();
          statucCollection.Add(status1);
          statucCollection.Add(status2);
  
          ddlStatus.DataTextField = "StatusDescription";
          ddlStatus.DataValueField = "StatusCode";
          ddlStatus.DataSource = statucCollection;
          ddlStatus.DataBind();
  
          //Assembly Version - 3.5.0.0 
          string version = System.Reflection.Assembly
                  .GetExecutingAssembly()
                  .GetReferencedAssemblies()
                  .Where(x =&gt; x.Name == "System.Core").First().Version.ToString();
  
          Response.Write(version);
  
      }
  
      string selectedVal = ddlStatus.SelectedItem.Value;
      int y = 0;
  }
  }
  
  public class Status
  {
  public int StatusCode { get; set; }
  public string StatusDescription { get; set; }
  }
  </code></pre>
  
  <p><strong>MARKUP</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormattingTest.aspx.cs" Inherits="FormattingTest"
  EnableViewState="false"  %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="true" EnableViewState="true" &gt;
  &lt;/asp:DropDownList&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Reference</strong>:</p>
  
  <ol>
  <li><a href="http://support.microsoft.com/kb/316813" rel="nofollow noreferrer">Server controls persist their state when EnableViewState is set to False</a></li>
  <li><a href="https://stackoverflow.com/questions/5485850/minimizing-viewstate-confused-by-enableviewstate-and-viewstatemode-in-asp-n">Minimizing viewstate- confused by `EnableViewState` and `ViewStateMode` in asp.net 4.0</a></li>
  <li><a href="https://stackoverflow.com/questions/11786981/enableviewstate-not-working-in-dropdownlist">EnableViewState not working in DropDownList</a></li>
  </ol>