PDA

View Full Version : how to use viewstate?



jachnicky
Mar 25, 2009, 11:59 PM
hi,

guys,how to use viewstate in the program?I use viewstate to store a datatable,but I can't get the result in another method or event.

geoffrey.mcgill
Mar 26, 2009, 1:10 AM
I don't understand what you are asking for. Can you provide a simplified .aspx code sample demonstrating how to reproduce the problem?

jachnicky
Mar 26, 2009, 3:47 AM
for example,I store a DataTable by ViewState:


protected void LoadDetails(object sender, AjaxEventArgs e)
{
string strSqlConn = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
SqlConnection sqlconn = new SqlConnection(strSqlConn);
sqlconn.Open();
String strSql = String.Empty;
strSql="select * from datatable";
DataSet ds_ProcessList = new DataSet();
SqlCommand cmd = new SqlCommand(strSql, sqlconn);
SqlDataAdapter sqlcmd = new SqlDataAdapter();
sqlcmd.SelectCommand = cmd;
sqlcmd.Fill(ds_ProcessList);
ViewState["DTNODES"]=ds_ProcessList.Tables[0];
}

I use the ViewState in another method:


protected void NodeDetailsStore_Submit(object sender, StoreSubmitDataEventArgs e)
{
DataTable dt_nodes=(DataTable)ViewState["DTNODES"];
}

the DataTable dt_nodes is null,why?

best regards

geoffrey.mcgill
Mar 26, 2009, 4:13 AM
By default ViewState is not transfered during an AjaxEvent. You can include the ViewState by setting ViewStateMode="Include".

Example



<Click OnEvent="LoadDetails" ViewStateMode="Include" />


Hope this helps.

jachnicky
Mar 26, 2009, 4:50 AM
yes,it works,thanks very much! :)