[英]How to prevent asp.net page from reseting data table?
Consider this code 考虑这段代码
DataTable dt = new DataTable() ;
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("RowIndex");
dt.Columns.Add("FilterText");
}
I will Fill this data table by some data during executing program. 在执行程序期间,我将用一些数据填充此数据表。 but when i click a button , the page will reload the codes and my data will be lost. 但是,当我单击按钮时,页面将重新加载代码,并且我的数据将丢失。 how can i prevent the web form from creating new data table and save my data? 如何防止Web表单创建新数据表并保存数据?
my controls are inside an update panel. 我的控件位于更新面板中。
A new instance of the class is created for each request. 将为每个请求创建一个新的类实例。 If you want to save things from one request to another you need to use a different mechanism. 如果要将内容从一个请求保存到另一个请求,则需要使用其他机制。
For example a global. 例如全球。 But on top of a globals usual problems you need to handle multiple concurrent requests and thus multi-threading and locking. 但是,除了全局问题之外,您还需要处理多个并发请求,从而需要多线程和锁定。
The usual approach is to store persistent state in a database, loading the information for each request at the start of processing that request. 通常的方法是将持久状态存储在数据库中,并在处理该请求的开始时为每个请求加载信息。
You can use View State to solve problem.. 您可以使用View State解决问题。
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("RowIndex");
dt.Columns.Add("FilterText");
}
else
{
if (ViewState["vsDT"] != null)
dt= ViewState["vsDT"] as DataTable;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.