[英]Better way to implement this rather than storing a value in Session?
在我的一個文件中,我允許用戶選擇開始和結束日期:
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);
}
在同一個文件中,我有一個驗證函數,所以如果結束日期小於開始日期,用戶將看到一條錯誤消息。
但是,我有另一個控件(在不同的文件中),如果第一個文件中的驗證失敗,我不希望保存該控件。
第一個文件:
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;
}
第二個文件:
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);
}
}
有沒有比使用 Session 存儲“True”或 Null 更好的方法來實現這一點?
您必須將該數據保存在某處。 常見位置將在會話變量中(就像您正在做的那樣),或者在每個請求的查詢字符串中傳遞它,或者在數據庫中讀取/寫入它。 他們都有優點和缺點。 例如,查詢字符串方法可以創建可能有問題的長 url,但也意味着如果用戶將其添加為書簽,則“狀態”將保存在 url 中。
會話變量與其他任何事物一樣常見,並且是執行您正在嘗試的操作的合理方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.