简体   繁体   English

实现这一点而不是在 Session 中存储值的更好方法?

[英]Better way to implement this rather than storing a value in Session?

In one of my files, I allow the user to select start and end date:在我的一个文件中,我允许用户选择开始和结束日期:

public DateTime? CourseStartDate
        {
            get => PDConvert.ToDateTime(this.tbCourseStartDate.SelectedDate);
            set => this.tbCourseStartDate.SelectedDate = PDConvert.ToDateTimeN(value);
        }

        public DateTime? CourseEndDate
        {
            get => PDConvert.ToDateTime(this.tbCourseEndDate.SelectedDate);
            set => this.tbCourseEndDate.SelectedDate = PDConvert.ToDateTimeN(value);
        }

In this same file,I have a validate function so if end date is less than start date, the user will see an error message.在同一个文件中,我有一个验证函数,所以如果结束日期小于开始日期,用户将看到一条错误消息。

However, I have another control (in a different file) where if validation fails in the first file, I don't want the control to save.但是,我有另一个控件(在不同的文件中),如果第一个文件中的验证失败,我不希望保存该控件。

First File:第一个文件:

private bool Validate()
    {
        if ((this.CourseStartDate != null && this.CourseEndDate != null) && (this.CourseEndDate < this.CourseStartDate))
        {
            lblError.Text = "Course Start Date must be same as or earlier than Course End Date";
            Session["CourseDates"] = "True";
            return false;
        }
        else
        {
            Session["CourseDates"] = null;
        }

        return true;

    }

Second File:第二个文件:

 public void Save()
        {
            if (Session["CourseDates"] == null)
            {
                HistoricalMark marks = new BLLHistMarks().GetHistoricalMarkByRecordId(this.HistStudentMarkId);

                foreach (GridViewRow row in this.gvMarks.Rows)
                {

                    string markType = (row.FindControl("hfType") as HiddenField).Value;
                    string value = (row.FindControl("tbMark") as TextBox).Text;
                    marks.SetMark((int)PDConvert.ToEnum(markType, TMarkType.Other), value);
                }

                new BLLHistMarks().UpdateHistoricalMarkMarks(marks);
            }
        }

Is there a better way to achieve this rather than using Session to store "True" or Null?有没有比使用 Session 存储“True”或 Null 更好的方法来实现这一点?

You have to persist that data somewhere .您必须将该数据保存在某处 Common locations would be in the session variable (like you're doing), or pass it through in the query string with each request, or read / write it in a database.常见位置将在会话变量中(就像您正在做的那样),或者在每个请求的查询字符串中传递它,或者在数据库中读取/写入它。 They all have pros and cons.他们都有优点和缺点。 Query string approach, for example can create long urls that can be problematic, but also means that the 'state' is saved in the url if the user bookmarks it.例如,查询字符串方法可以创建可能有问题的长 url,但也意味着如果用户将其添加为书签,则“状态”将保存在 url 中。

Session variable is as common as anything else, and is a reasonable way to do what you're trying.会话变量与其他任何事物一样常见,并且是执行您正在尝试的操作的合理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM