在一个会话中获取多个项目

时间:2012-04-27 22:45:55

标签: c# asp.net

我将多个项目存储到一个会话变量中:这是4个不同的单词。

我需要能够在一个会话中获取这4个不同的单词,并将它们存储到单独的变量中以便我可以使用它们,这可能吗?如果是这样的话?

_application只保存文本框中一系列文本的文本。

这是我的会话课。

 public class JobApplicantSession
{

    public JobApplication ApplicationSession
    {

      get {if (HttpContext.Current.Session["Application"] != null)
               return (JobApplication)HttpContext.Current.Session["Application"];
           return null; }

      set{ HttpContext.Current.Session["Application"] = value; }
    }

作业应用程序:作业应用程序保存来自文本框列表的信息。

JobApplication _application;
_application = new JobApplication(jobID);
if (i < _applicant.Fields.Count)
                _applicant.AddAnswer((_controlsList[i] as TextBox).Text);
            else
                _application.AddAnswer((_controlsList[i] as TextBox).Text);

添加到会话中:

_sessions.ApplicationSession = _application;

我可以参加会议:

JobApplication application;
var jas = new JobApplicantSession();
application = jas.ApplicationSession;

工作申请:

public class JobApplication
{
    private int _jobID = -1;
    public JobApplication(int jobID)
    {
        _jobID = jobID;
    }
    List<String> _answers = new List<String>();
    List<JobApplicationField> _fields = new List<JobApplicationField>();
    public List<JobApplicationField> Fields
    {
        get { return _fields; }
    }
    public int JobID()
    {
        return _jobID;
    }
    public List<String> Answers
    {
        get { return _answers; }
        set { _answers = value; }
    }
    public void AddAnswer(String answer)
    {
        _answers.Add(answer);
    }
    public void AddField(JobApplicationField field)
    {
        if (field.HasFieldID)
        {
            for (int i = 0; i < _fields.Count(); i++)
            {
                if (_fields[i].FieldID == field.FieldID)
                {
                    _fields[i] = field;
                    return;
                }
            }

        }

        _fields.Add(field);
    }
    public void PopulateFromDatabase()
    {
        JobPositionSystemDAL dal = new JobPositionSystemDAL();
        DataSet data = dal.OpenJobOpeningByID(_jobID);
        foreach (DataRow row in data.Tables[0].Rows)
        {
            JobApplicationField field = new JobApplicationField(Convert.ToInt32(row["QUESTIONID"]), row["QUESTIONTEXT"].ToString(), Convert.ToBoolean(row["HASCORRECTANSWER"]), row["CORRECTANSWER"].ToString(), Convert.ToBoolean(row["ISREQUIRED"]));
            AddField(field);
        }
    }
    public bool Verify()
    {
        if (_fields.Count != Answers.Count)
            throw new Exception("Number of answers != number of fields");
        for (int i = 0; i < _fields.Count; i++)
        {
            if (_fields[i].HasCorrectAnswer == true && _fields[i].CorrectAnswer != _answers[i])
                return false;
        }
        return true;
    }

    public void Save()
    {
        JobPositionSystemDAL database = new JobPositionSystemDAL();

        foreach(JobApplicationField field in _fields)
        {
            field.Save(_jobID, ref database);
        }

        //reload to get the Field/question ID for any new fields
        Reload();
    }

    public void Reload()
    {
        _fields = new List<JobApplicationField>();
        Load(_jobID);
    }

    public void Load(int jobID)
    {
        _jobID = jobID;
        DataSet ds = DB.OpenJobOpeningByID(jobID);
        DataTable table = ds.Tables[0];

        foreach (DataRow row in table.Rows)
        {
            int questionID = 0;
            object obj = row["QUESTIONID"];
            Int32.TryParse(obj.ToString(), out questionID);
            //int jobApplicationID = Int32.Parse(row["JOBAPPLICATIONID"] as String);
            string questionText = row["QUESTIONTEXT"] as String;
            int type = Int32.Parse(row["TYPEID"].ToString());
            bool hasCorrectAnswer = (row["HASCORRECTANSWER"] as String == "0") ? false : true;
            string correctAnswer = row["CORRECTANSWER"] as String;
            bool required = ((row["ISREQUIRED"] as String) == "0") ? false : true;

            JobApplicationField field = new JobApplicationField(questionID, questionText, hasCorrectAnswer, correctAnswer, required);
            AddField(field);
        }
    }
    private JobPositionSystemDAL _db;
    private JobPositionSystemDAL DB 
    {
        get
        {
            if (null == _db)
                _db = new JobPositionSystemDAL();
            return _db;
        }
    }
}
}

如何循环并将每个项目存储到自己的变量中? 我需要4个变量。

1 个答案:

答案 0 :(得分:0)

希望这就是你所要求的。如果要以会话状态存储对象数组,可以直接使用数组:

 HttpContext.Current.Session["data"] = new int[32]; 

或根据元素索引动态创建名称(奇怪但可能):

 var data = new int[3];
 HttpContext.Current.Session["data" + 0.ToString()] = data[0]; 
 HttpContext.Current.Session["data"  + 1.ToString()] = data[1]; 
 HttpContext.Current.Session["data"  + 2.ToString()] = data[2]; 

按相反顺序阅读:

var dataArray = (int[])(HttpContext.Current.Session["data"]);

 var data = new int[3];
 data[0] = (int)HttpContext.Current.Session["data" + + 0.ToString()];