我正在使用 Visual Studio 2010 创建一个 Web 表单,并且我有一个 .aspx 页面,它可以获取一些用户输入并将其保存到数据库中。页面上有两个按钮,一个用于用户“保存”值(应在此处添加到列表中),第二个用于以正确的格式将它们实际保存到数据库中。我的代码如下:
namespace TabletApplication.HomePages
{
public partial class Record : System.Web.UI.Page
{
List<ReadingModel> paramList;
protected void Page_Load(object sender, EventArgs e)
{
paramList = new List<ReadingModel>();
}
public class ReadingModel
{
public int paramId { get; set; }
public string value { get; set; }
public Boolean pass { get; set; }
public string comment { get; set; }
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
// Get an instance of the Button
Button UpdateButton = (Button)sender;
// Get the ID of the current record from the CommandArgument
int intID = Convert.ToInt32(UpdateButton.CommandArgument);
bool goodValue;
using (TabletApplicationEntities serverContext = new TabletApplicationEntities())
{
// Get the value and comment
TextBox Reading =
(TextBox)UpdateButton.Parent.FindControl("txtReading");
TextBox Comment =
(TextBox)UpdateButton.Parent.FindControl("txtComment");
//Get min and max values
Label Min = (Label)UpdateButton.Parent.FindControl("minValLabel");
Label Max = (Label)UpdateButton.Parent.FindControl("maxValLabel");
double minVal = Convert.ToDouble(Min.Text);
double maxVal = Convert.ToDouble(Max.Text);
int readingVal = -1;
try
{
readingVal = Convert.ToInt32(Reading.Text);
goodValue = true;
}
catch
{
Reading.Text = "Invalid entry: Must enter a number";
Reading.BackColor = System.Drawing.Color.Red;
Reading.ForeColor = System.Drawing.Color.Blue;
goodValue = false;
}
if (goodValue)
{
// Update the record
ReadingModel Result = new ReadingModel();
Result.comment = Comment.Text;
Result.value = Reading.Text;
Result.paramId = intID;
if (readingVal > minVal && readingVal < maxVal && readingVal >= 0)
{
Result.pass = true;
}
else if (readingVal < minVal || readingVal > maxVal)
{
Result.pass = false;
}
else
{
Reading.Text = "UNKNOWN ERROR: Try again";
Reading.BackColor = System.Drawing.Color.Red;
Reading.ForeColor = System.Drawing.Color.Blue;
}
paramList.Count();
paramList.Add(Result);
paramList.Count();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (TabletApplicationEntities serverContext = new TabletApplicationEntities())
{
// Create a new Reading
foreach (var para in paramList)
{
Reading objReading = new Reading();
// Set values
objReading.ParamID = para.paramId;
objReading.comment = para.comment;
objReading.date = System.DateTime.Now;
objReading.pass = para.pass;
objReading.reading1 = para.value;
// Add object
serverContext.Readings.AddObject(objReading);
}
// Save changes
serverContext.SaveChanges();
paramList.Clear();
}
}
}
}
当用户点击第一个按钮时,该btnUpdate_Click
函数正常运行,并将新的添加Result
到paramList
. 在这两个断点上,paramList.Count()
我可以看到在添加结果之前计数为 0,之后为 1,就像它应该的那样。但是,当按下保存按钮时,btnSubmit_Click
函数运行并完全跳过 for 循环,因为计数paramList
再次为 0。有任何想法吗?
编辑1:
我尝试将 ViewState 添加到 Page_Load,但我相信它不起作用,因为 List 不是“可序列化的”,但我不知道如何修复它。这是我添加的:
List<ReadingModel> paramList;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["paramList"] != null)
{
paramList = (List<ReadingModel>)ViewState["paramList"];
}
else
{
paramList = new List<ReadingModel>();
}
}