C# 当显示CrossPage1.aspx时,在传输到CrossPage2页面之前,我需要在列表框中进行相同的选择,但仍然必须选择该页面

C# 当显示CrossPage1.aspx时,在传输到CrossPage2页面之前,我需要在列表框中进行相同的选择,但仍然必须选择该页面,c#,cross-page-postback,C#,Cross Page Postback,我有一个首页(EmployeeDetails),其中有一些文本框和一个列表框,在添加文本值后将填充它们。当我导航到另一个显示所选列表框项目详细信息的页面(SpecificDetails)时,我需要让列表框保持填充状态。 我在第二页(SpecificDetails)上添加了一个按钮,该按钮应该返回到第一页(EmployeeDetails),而第一页的数据仍然显示 以下是每个项目的代码: 员工详细信息 using System; using System.Collections.Generic; u

我有一个首页(EmployeeDetails),其中有一些文本框和一个列表框,在添加文本值后将填充它们。当我导航到另一个显示所选列表框项目详细信息的页面(SpecificDetails)时,我需要让列表框保持填充状态。 我在第二页(SpecificDetails)上添加了一个按钮,该按钮应该返回到第一页(EmployeeDetails),而第一页的数据仍然显示

以下是每个项目的代码: 员工详细信息

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


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
公共部分类EmployeeDetails:System.Web.UI.Page
{
私人名单雇员;
受保护的无效页面加载(对象发送方、事件参数e)
{
if(this.IsPostBack)
{
员工=(列表)会话[“emp”];
}
其他的
{
雇员=新名单();
如果(员工!=null)
{
updateListBox();
int sNum=-1;
Int32.TryParse(Request.Params[“Social”],out sNum);
如果(sNum>=0)
{
lstmemployees.SelectedIndex=sNum;
}
}
else employees=新列表();
}
}
受保护的无效页面\u预呈现(对象发送方,事件参数e)
{
会话[“emp”]=员工;
}
受保护的void updateListBox()
{
lstmemployees.Items.Clear();
foreach(员工中的员工e)
{
lstmemployees.Items.Add(e.Social);
}
}
受保护的无效btnAdd_单击(对象发送者,事件参数e)
{
int snum;
内阴囊;
if((Int32.TryParse(txtcarly.Text,out snum)和&(Int32.TryParse(txtCost.Text,out ynum)
&&txtName.Text.Length>0)
{
添加(新员工(txtName.Text,snum,ynum));
updateListBox();
lblError.Text=“”;
lblError.Text=employees.Count().ToString();
}
其他的
lblError.Text=“输入有效的SSN编号、雇佣年份、雇佣年份结束。”;
txtName.Text=“”;
txtSalary.Text=“”;
txtCost.Text=“”;
}
受保护的无效btnRemove\u单击(对象发送者,事件参数e)
{
如果(lstEmployees.SelectedIndex==-1)
lblError.Text=“未选择任何员工。”;
其他的
{
Employee emp=员工[lstEmployees.SelectedIndex];
employees.RemoveAt(lstEmployees.SelectedIndex);
updateListBox();
lblError.Text=“员工:”+emp.Social;
lblError.Text+=“
已被删除。”; } } 受保护的void btnRemoveAll\u单击(对象发送方,事件参数e) { 雇员; updateListBox(); lblError.Text=“已删除所有员工。”; } 受保护的void cmdMoreInfo\u单击(对象发送方,事件参数e) { 如果(lstEmployees.SelectedIndex==-1) { lblError.Text=“未选择任何员工。”; } 其他的 Server.Transfer(“SpecificEmployee.aspx?emp=“+lstEmployees.SelectedIndex,true”); } 公共字符串AllEmp { 获取{return txtName.Text+“
”+txtmarally.Text+“
”+txtCost.Text;} } }
这是第二页:(具体细节)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
公共部分类特定员工:System.Web.UI.Page
{
私人名单雇员;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(上一页!=null)
{
雇员=新名单();
员工=(列表)会话[“emp”];
int i=-1;
Int32.TryParse(Request.Params[“emp”],out i);
员工环境管理计划;
emp=员工[i];
//lblEmployee.Text+=“这是您的条目:“+prevPage.AllEmp+”
”; lblEmployee.Text+=“员工:”+emp.getDisplayText()+ “
”; } } 受保护的void cmdReturn\u单击(对象发送方,事件参数e) { 重定向(“EmployeeDetails.aspx?AllEmp”); } }
if(this.IsPostBack)
{
员工=(列表)会话[“emp”];
}
只有当您来自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;
        }
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
员工=(列表)会话[“emp”];
if(employees==null)
{ 
雇员=新名单();
}
其他的
{
updateListBox();
int sNum=-1;
Int32.TryParse(Request.Params[“Social”],out sNum);
如果(sNum>=0)
{
lstmemployees.SelectedIndex=sNum;
}
}
}

我真的需要帮助编码cmdReturn\u Click按钮。我想返回到上一页,让列表框中以前选择的内容仍然显示出来。我刚刚添加了.cs代码。如果有人需要aspx.cs代码,请告诉我。唯一真正重要的是验证。感谢-Tom2分配员工…第一个是新的list then with session…和ispostback只有在您来自同一页面时才应为真…那么您是说我需要删除第2页上的一项任务,然后将第一页上的Is Postback更改为=真?不确定该页面上的代码…也不确定您试图实现的目标…您尝试通过会话…表示您的一个视图创建了该列表(或为
 if (this.IsPostBack)
 {
        employees = (List<Employee>)Session["emp"];
 }
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;
        }
    }
}