0

I have a front page (EmployeeDetails) with some text boxes and a listbox that will populate after adding text values. I need to have the listbox stay populated as I navigate to another page (SpecificDetails) that shows the details of the selected listbox item. I the have added a button on the second page (SpecificDetails) that is supposed to return to a first page (EmployeeDetails while having the data from the first page still showing.

Here is the code for each: Employees Details

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EmployeeDetails : System.Web.UI.Page
{
    private List<Employee> employees;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            employees = (List<Employee>)Session["emp"];
        }
        else
        {

           employees = new List<Employee>();
            if (employees != null)
            {
                updateListBox();
                int sNum = -1;
                Int32.TryParse(Request.Params["Social"], out sNum);
                if (sNum >= 0)
                {
                    lstEmployees.SelectedIndex = sNum;
                }
            }
            else employees = new List<Employee>();
        }


    }
    protected void Page_PreRender(Object sender, EventArgs e)
    {
        Session["emp"] = employees;
    }

    protected void updateListBox()
    {
        lstEmployees.Items.Clear();


        foreach (Employee e in employees)
        {
            lstEmployees.Items.Add(e.Social);

        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
       int snum;
       int ynum;


        if ((Int32.TryParse(txtSalary.Text, out snum) && (Int32.TryParse(txtCost.Text, out ynum) 
            && txtName.Text.Length > 0)))
        {

            employees.Add(new Employee(txtName.Text, snum, ynum));
            updateListBox();
            lblError.Text = "";
            lblError.Text = employees.Count().ToString();           

        }
        else
            lblError.Text = "Enter valid SSN Number, Year of Hire, Year of Employment End.";
            txtName.Text = "";
            txtSalary.Text = "";
            txtCost.Text = "";



}

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (lstEmployees.SelectedIndex == -1)
            lblError.Text = "No Employee Selected.";
        else
        {
            Employee emp = employees[lstEmployees.SelectedIndex];

            employees.RemoveAt(lstEmployees.SelectedIndex);
            updateListBox();
            lblError.Text = "Employee: " + emp.Social;
            lblError.Text += "<br />has been removed. ";

        }

    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        employees.Clear();
        updateListBox();
        lblError.Text = "All Employees Removed.";

    }

    protected void cmdMoreInfo_Click(object sender, EventArgs e)
    {
        if (lstEmployees.SelectedIndex == -1)
        {
            lblError.Text = "No employee selected.";
        }
        else

            Server.Transfer("SpecificEmployee.aspx?emp=" + lstEmployees.SelectedIndex, true);
    }

    public string AllEmp
    {
       get { return txtName.Text + "<br />" + txtSalary.Text + "<br />"+ txtCost.Text; }
    }


}

Here is the second page: (SpecificDetails)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SpecificEmployee : System.Web.UI.Page
{
    private List<Employee> employees;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {

            employees = new List<Employee>();
            employees = (List<Employee>)Session["emp"];


            int i = -1;
            Int32.TryParse(Request.Params["emp"], out i);

            Employee emp;
            emp = employees[i];

            // lblEmployee.Text += "Here is Your Entry: " + prevPage.AllEmp + "<br />";
            lblEmployee.Text += "Employee: " + emp.getDisplayText() +
            "<br />";
        }


    }

    protected void cmdReturn_Click(object sender, EventArgs e)
    {


        Response.Redirect("EmployeeDetails.aspx?AllEmp" );

    }

}
  • I really need help coding the cmdReturn_Click button. I want to go back to the previous page and have what was selected in Listbox previously still show. – TMiller24 Apr 17 '18 at 04:05
  • I have just added the.cs code. Let me know if anyone needs the aspx.cs code as well. Only thing really of significance is validations. Thanks-Tom – TMiller24 Apr 17 '18 at 04:06
  • 2 assignments of employee... 1st with new list then with session... and ispostback should only be true when you come from the same page... – Florian Schmidinger Apr 17 '18 at 04:44
  • So are you saying that I need to eliminate one of the assignments on Page 2,then change the Is Postback on the first page to = true? Not sure of the code on that... – TMiller24 Apr 18 '18 at 02:08
  • not sure what you are trying to achieve either... you try to share a list of employee via session... means one of your views creates that list (or loads it from the database, etc) and the other view just displays it? btw this is programming stateful and that is something you should avoid at great cost. – Florian Schmidinger Apr 18 '18 at 11:35
  • Here's the gist of it. I have 3 text boxes to put info into. Then I click a button to add the 3 text box parameters to the list box, only displaying one parameter (Social Security Number) Then I have another button that I click that navigates to a second page which displays all the text box info via the selected index of the listbox in text form (in a label). What I am struggling with, is that I need the information on the first page to stay stored there, as on the second page there is a return button to go back to first page, which should still display the selected item in the listbox. – TMiller24 Apr 18 '18 at 22:46
  • keyword here seems 'stored'... how will you persist the data, it cannot stay in the session forever... – Florian Schmidinger Apr 19 '18 at 14:23
  • Hence the problem, I wanted to try to use the session or a query string, but did not know how to do that properly. – TMiller24 Apr 19 '18 at 23:54

1 Answers1

0
 if (this.IsPostBack)
 {
        employees = (List<Employee>)Session["emp"];
 }

will only be true if you come from the EmployeeDetails page itself.

this however should work for your EmployeeDetails:

protected void Page_Load(object sender, EventArgs e)
{
    employees = (List<Employee>)Session["emp"];
    if(employees == null)
    { 
        employees = new List<Employee>();
    }
    else
    {
        updateListBox();
        int sNum = -1;
        Int32.TryParse(Request.Params["Social"], out sNum);
        if (sNum >= 0)
        {
            lstEmployees.SelectedIndex = sNum;
        }
    }
}
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28