1

我将多个项目存储到一个会话变量中:这是 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 个变量。

4

1 回答 1

0

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

 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()]; 
于 2012-04-27T23:09:06.893 回答