user2007117
user2007117

Reputation:

How to override a session

I have a question can I overwrite a session that I allredy have set (I set the the user specific department)

Session:

 int depId = user.HtDepartments.FirstOrDefault().DepartmentId;  
 Session["DepId"] = depId;

I want to override this id that I saved in the session with a new "id" that the user selectes out of the RadComboBox. So when the user is coming back to this page not his "pre selected" department is selected , selected is the new department that he slected before

My code so far:

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();
    }
}

Loading the session

Here how I load my department

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();
        }

A resume of my question:

How and where should I override my session in the load method or in SelectedIndexChanged ?

Upvotes: 1

Views: 1125

Answers (1)

Amicable
Amicable

Reputation: 3101

It makes sense to check for the saved variable when loading a page, as the user won't be able to interact with the controls until this method has completed.

private void OnLoad(object sender, EventArgs e)
{
    this.parameterDepId = this.Request["depId"];
}

But to override it with a new value you simply set a new value. Then the next time you loaded the page this newly set value would be returned in the OnLoad method.

private void MyControl_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["DepId"] = MyControl.SelectedValue;
}

I'm finding your question a bit unclear but hopefully this answers the main point of it and helps with your code logic.

Upvotes: 1

Related Questions