从usercontrol的会话变量不会加载,直到刷新
问题描述:
我是新来的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"];
}
}
}
答
您设置会话变量的最早点可能是罪魁祸首。在Session_Start中的Global.asax文件中设置一个会话变量,以便您的会话将从应用程序启动持续直到您销毁它。像Session [“start”] = DateTime.Now;
您在控件的生命周期中加载会话的位置?换句话说,哪个函数(Page_Load,Page_Init等)? – 2013-02-15 23:20:45
您需要挂钩在代码运行时引发的事件,在C#WinForms中它是Form_Loaded事件,尽管我不确定ASP.NET。一旦你设法找到它,你只需调用刷新时调用的代码即可。对不起,我忍不住了! – XtrmJosh 2013-02-15 23:20:55
@SystemDown在Page_Load上。 – Zach 2013-02-15 23:23:35