Session Variable from usercontrol not loading until refresh
<p>I'm new to asp.net and was wondering the correct way to handle this problem.</p>

<p>When a user logs in they are sent to a dashboard. I have a usercontrol in my master page that loads automatically the users account into a session variable (it is a dropdownlist so they can choose multiple accounts).</p>

<p>My dashboard has the code below</p>

<pre><code>if (Session["SiteID"] != null)
{
    SiteID = int.Parse(Session["SiteID"].ToString());
    PopulateAccountData();
    PopulateAccountInformation2();
    PopulateSiteNodes();
    PopulateSiteMap();
}

else
{
    LabelSiteName.Text = "No Site Selected";

}
</code></pre>

<p>On first load the page always shows No Site selected. If I hit refresh it populates the page correctly.</p>

<p>How do I get it to use the session variables when it first loads?</p>

<p>updates - This is my usercontrol 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;
using System.Data;

namespace WebApp
{
public partial class SitePicker : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ClientList;
        // if never loaded, go get it and stash it
        if (Session["dsClientList"] == null)
        {
            ClientList = GetClientListFromDB();
            Session["dsClientList"] = ClientList;
            Session["ClientID"] = (int)ClientList.Tables[0].Rows[0]["ClientID"];
        }
        else
        {
            ClientList = (DataSet)Session["dsClientList"];
        }

        if (DropDownListClient.Items.Count == 0)
        {
            DropDownListClient.DataSource = ClientList;
            DropDownListClient.DataValueField = "ClientID";
            DropDownListClient.DataTextField = "ClientName";
            DropDownListClient.DataBind();
        }
        //ClientButton.Text = DropDownListClient.SelectedItem.Text;

        DataSet SiteList;
        if (Session["dsSiteList"] == null)
        {
            SiteList = GetSiteListFromDB();
            Session["dsSiteList"] = SiteList;
            Session["SiteID"] = (int)SiteList.Tables[0].Rows[0]["SiteID"];
        }
        else
            SiteList = (DataSet)Session["dsSiteList"];

        if (DropDownListSite.Items.Count == 0)
        {
         //   SiteButton.Text = DropDownListSite.SelectedItem.Text;
            DropDownListSite.DataSource = SiteList;
            DropDownListSite.DataValueField = "SiteID";
            DropDownListSite.DataTextField = "SiteName";
            DropDownListSite.DataBind();
            DropDownListSite.SelectedValue = Session["SiteID"].ToString();
        }
    }

    protected void LoadSite()
    {
//            Response.Redirect(Request.RawUrl);
//            Response.Redirect(".");
        Response.Redirect(Request.RawUrl, true);
        //Server.Transfer(".");
    }

    protected void ClientButton_Click(object sender, EventArgs e)
    {
        //DropDownListClient.Visible = true;
        //ClientButton.Visible = false;
    }

    DataSet GetClientListFromDB()
    {
        DataAccess da = new DataAccess();
        da.AddParameter("portaluserid", Page.User.Identity.Name, DataAccess.SQLDataType.SQLString, 256);
        DataSet ClientList = da.runSPDataSet("Portal_SitePickerClientList");
        return ClientList;
    }

    protected void DropDownListClient_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ClientButton.Enabled = true;
        //DropDownListClient.Visible = false;
        int ClientID = int.Parse(DropDownListClient.SelectedItem.Value);
        ClientButton.Text = DropDownListClient.SelectedItem.Text;
        //ClientButton.Visible = true;
        Session["ClientID"] = ClientID;
        Session["ClientName"] = DropDownListClient.SelectedItem.Text;
        PopulateSiteList();
    }

    DataSet GetSiteListFromDB()
    {
        DataAccess da = new DataAccess();
        da.AddParameter("portaluserid", Page.User.Identity.Name, DataAccess.SQLDataType.SQLString, 256);
        da.AddParameter("ClientID", Session["ClientID"], DataAccess.SQLDataType.SQLInteger, 4);
        DataSet SiteList = da.runSPDataSet("Portal_SitePickerSiteList");
        return SiteList;
    }

    void PopulateSiteList()
    {
//            DataSet SiteList;
        //if (Session["dsSiteList"] != null)
        //    SiteList = (DataSet)Session["dsSiteList"];
        //else
        //    SiteList = GetSiteListFromDB();
        //DropDownListSite.DataSource = SiteList;
        //DropDownListSite.DataValueField = "SiteID";
        //DropDownListSite.DataTextField = "SiteName";
        //DropDownListSite.DataBind();
        //DropDownListSite.Visible = true;
        //SiteButton.Visible = false;
        //DropDownListSite.SelectedIndex = 1;
    }

    protected void DropDownListSite_SelectedIndexChanged(object sender, EventArgs e)
    {
        //DropDownListSite.Visible = false;
        //SiteButton.Visible = true;
        int SiteID = int.Parse(DropDownListSite.SelectedItem.Value);
        SiteButton.Text = DropDownListSite.SelectedItem.Text;
        Session["SiteID"] = SiteID;
        Session["SiteName"] = DropDownListSite.SelectedItem.Text;
        LoadSite();
    }

    protected void SiteButton_Click(object sender, EventArgs e)
    {
        //if (DropDownListSite.Visible == false)
            //PopulateSiteList();
        //SiteButton.Visible = false;
        if (Session["SiteID"] != null)
        {
            int SiteID = (int)Session["SiteID"];
            DropDownListSite.SelectedValue = SiteID.ToString();
        }
    }

    protected void GetClientAndSiteFromSiteID()
    {
        DataAccess da = new DataAccess();
        da.AddParameter("SiteID", (int)Session["SiteID"], DataAccess.SQLDataType.SQLInteger, 4);
        DataSet ds = da.runSPDataSet("NavigationGetClientSiteFromSiteID");
        Session["ClientID"] = (int)ds.Tables[0].Rows[0]["ClientID"];
        Session["ClientName"] = ds.Tables[0].Rows[0]["ClientName"];
        Session["SiteName"] = ds.Tables[0].Rows[0]["SiteName"];
    }

}
}
</code></pre>
