我有一个问题可以覆盖我已经设置的会话(我设置了用户特定的部门)
会话
int depId = user.HtDepartments.FirstOrDefault().DepartmentId;
Session["DepId"] = depId;
我想覆盖我在会话中保存的id,其中包含用户从RadComboBox中选择的新“id”。 因此,当用户回到此页面而不是他的“预选”部门时,选择的是他之前选择的新部门
到目前为止我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.parameterDepId = this.Request["depId"];
if (string.IsNullOrEmpty(parameterDepId))
{
parameterDepId = Session["depId"].ToString();
}
this.LoadDE(parameterDepId);
this.UserGrid.DataBind();
}
}
加载会话
这是我如何加载我的部门
protected void LoadDE(string depId)
{
IEnumerable<HtDepartment> departments = null;
if (this.selectedBu != null)
{
departments = this.selectedBu.HtDepartments;
}
this.rcbDepartments.DataTextField = "Name";
this.rcbDepartments.DataValueField = "DepartmentId";
this.rcbDepartments.DataSource = departments;
this.rcbDepartments.DataBind();
this.rcbDepartments.Items.Insert(0, new RadComboBoxItem("-All-"));
if (depId != null)
{
int index = this.rcbDepartments.Items.IndexOf(this.rcbDepartments.Items.Where(i => i.Value == depId).SingleOrDefault());
if (index > -1)
{
this.rcbDepartments.Items[index].Selected = true;
this.selectedDepartment = departments.Where(i => i.DepartmentId == int.Parse(depId)).SingleOrDefault();
}
}
}
protected void rcbDepatments_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
if (this.rcbDepartments.Items.Count > 1 && this.rcbDepartments.SelectedIndex > 0)
{
this.selectedDepartment = HtDepartment.GetById(int.Parse(e.Value));
parameterDepId = this.selectedDepartment.ToString();
}
else
{
this.selectedDepartment = null;
}
this.dataSourceFilterChanged();
}
我的问题的简历:
我应该在load方法或SelectedIndexChanged中覆盖会话的方式和位置?
答案 0 :(得分:1)
在加载页面时检查已保存的变量是有意义的,因为在此方法完成之前,用户将无法与控件进行交互。
private void OnLoad(object sender, EventArgs e)
{
this.parameterDepId = this.Request["depId"];
}
但要用新值覆盖它,您只需设置一个新值即可。然后,下次加载页面时,将在OnLoad方法中返回此新设置的值。
private void MyControl_SelectedIndexChanged(object sender, EventArgs e)
{
Session["DepId"] = MyControl.SelectedValue;
}
我发现你的问题有点不清楚,但希望这能解决它的主要问题,并有助于你的代码逻辑。