title: Retrieving values from DropDownList on Postback in asp.net
body: <p>I have a weird problem with the DropDownList postback.</p>
  
  <p>I have a DropDownList in asp.net master page, which contains some state names like :</p>
  
  <ol>
  <li>Text [NewYork] - Value [0]</li>
  <li>Text [New Jersey] - Value [1]</li>
  </ol>
  
  <p></p>
  
  <pre><code>drpTowns.DataSource = objTown.GetAllStates();
  drpTowns.DataTextField = "Name";
  drpTowns.DataValueField = "Id";
  drpTowns.DataBind();
  </code></pre>
  
  <p>In the code behind of the master page, i have a DropDownList_SelectedIndexChanged event, where i am setting the SelectedValue of the dropdownlist in a variable which is holding session. like below</p>
  
  <pre><code>protected void drpTowns_SelectedIndexChanged(object sender, EventArgs e)
  {
      Globals.DefaultTown = Convert.ToInt32(drpTowns.SelectedValue);
  }
  </code></pre>
  
  <p>Definition for Globals.DefaultTown is written in App_Code Globals.cs Class like below:</p>
  
  <pre><code> private static int _defaultTown = Convert.ToInt32(ConfigurationManager.AppSettings["DefaultTown"]);
  
  public static int DefaultTown
  {
      get
      {
          if (HttpContext.Current.Session["DefaultTown"] == null)
              return _defaultTown;
          else
              return Convert.ToInt32(HttpContext.Current.Session["DefaultTown"]);
      }
      set
      {
          HttpContext.Current.Session["DefaultTown"] = value;
      }
  }
  </code></pre>
  
  <p>Now i want to retrieve the value of the Globals.DefaultTown in Content Page (Default.aspx). I am doing that like below:</p>
  
  <pre><code>Response.Write("Default Town Is: " + Globals.DefaultTown + "&lt;br /&gt;");
  </code></pre>
  
  <p>Now whenever i selects the state from the dropdownlist, the Globals.DefaultTown does not updates immediately, like by default the Selected State is setted for DefaultTown, but when i selects second state from the list, it still gives the id of first state, now when i select third state from the list, it gives the id of the second, and when i select first state from the list, it gives the id of the third state, i.e it does not updates the DefaultTown variable on the spot.</p>
  
  <p>Can anyone tell me what would be going wrong for this</p>