Mar 19, 2012, 3:08 PM
use Viewstate to persist data in a page lifecycle. is it possible?
Hello,
Is it possible to use viewstate to persist data in a page lifecycle? I've been trying to use a viewstate to be the datasource of a json store which in turn the store become the source data for a GridPanel. Here's the the declaration
Every code in a ascx. which means this I put this viewstate not in a web page but in a user control. I only want to inform, just in case that would be a trouble.
Is it possible to use viewstate to persist data in a page lifecycle? I've been trying to use a viewstate to be the datasource of a json store which in turn the store become the source data for a GridPanel. Here's the the declaration
Every code in a ascx. which means this I put this viewstate not in a web page but in a user control. I only want to inform, just in case that would be a trouble.
public List<BarangDTO> titipanMasukItem
{
get
{
if (ViewState["titipanMasukItem"] != null)
{
return (List<BarangDTO>)ViewState["titipanMasukItem"];
}
return null;
}
set
{
ViewState["titipanMasukItem"] = value;
}
}
Right when the page is loaded, I instantiate the viewstate protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
....
....
titipanMasukItem = new List<BarangDTO>();
}
}
Then user can add new item to the grid manually, this is the code when user has clicked the add item button protected void ButtonAddItemWizard_Click(object sender, DirectEventArgs e)
{
string err = "";
Int32 result;
....
....
//if valid, add item togrid
titipanMasukItem.Add(new BarangDTO
{
id = ComboBoxBatchNoWizard.SelectedItem.Value,
itemid = ComboBoxItemNameWizard.SelectedItem.Value,
itemname = ComboBoxItemNameWizard.SelectedItem.Text,
itemdetailid = ComboBoxBatchNoWizard.SelectedItem.Value,
itembatchno = ComboBoxBatchNoWizard.SelectedItem.Text,
itemdescription = TextAreaDescriptionWizard.Text,
itemqty = Convert.ToInt32(TextFieldQtyWizard.Text)
});
this.StoreItemDetailWizard.DataSource = titipanMasukItem;
this.StoreItemDetailWizard.DataBind();
ClearItemForm();
}
Now, what bothering me is that, everytime the user clicked the add button. The viewstate seems to be always renewed. If I put a breakpoint right at the first line of the ButtonAddItemWizard_Click method. The count of the List (viewstate) is always 0. So, there will always be 1 item only everytime the user add a new item, where there should be a new item added to the GridPanel.