2013-02-15 96 views
1

我是新來的asp.net,想知道處理這個問題的正確方法。從usercontrol的會話變量不會加載,直到刷新

當用戶登錄時,它們被髮送到儀表板。我在我的母版頁中有一個用戶控件,它自動將用戶帳戶加載到會話變量中(它是一個下拉列表,以便他們可以選擇多個帳戶)。

我的儀表盤下面有

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

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

} 

代碼第一次加載的頁面始終顯示未選擇網站。如果我點擊刷新,它會正確填充頁面。

如何在第一次加載時使用會話變量?

updates - 這是我的usercontrol代碼。

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"]; 
    } 

} 
} 
+0

您在控件的生命週期中加載會話的位置?換句話說,哪個函數(Page_Load,Page_Init等)? – 2013-02-15 23:20:45

+0

您需要掛鉤在代碼運行時引發的事件,在C#WinForms中它是Form_Loaded事件,儘管我不確定ASP.NET。一旦你設法找到它,你只需調用刷新時調用的代碼即可。對不起,我忍不住了! – XtrmJosh 2013-02-15 23:20:55

+0

@SystemDown在Page_Load上。 – Zach 2013-02-15 23:23:35

回答

1

這裏的問題:一個母版頁加載用戶控件之前。這意味着主頁中的Page_Load事件發生在用戶控件中的Page_Load事件之前。因此,當您嘗試閱讀該會話時,該會話尚未填充。

一個解決方案是在ASP.NET生命週期中選擇稍後的事件。我會選擇PreRender事件,因爲它發生在Load事件之後。換句話說,主頁中的Page_PreRender發生在用戶控件中的Page_Load之後。請參考this ASP.NET生命週期的超詳細地圖。

+0

謝謝。我將它改爲PreRender,效果很好。感謝您的詳細地圖。我從中學到了一堆。 – Zach 2013-02-18 01:03:33

0

您設置會話變量的最早點可能是罪魁禍首。在Session_Start中的Global.asax文件中設置一個會話變量,以便您的會話將從應用程序啓動持續直到您銷燬它。像Session [「start」] = DateTime.Now;