I got a sample code from the site. One thing that I just don’t understand about how the value is added to ViewStateautomatically.
code as follows
private Dictionary<Guid, string> Names
{
get { return (Dictionary<Guid, string>)ViewState["Names"]; }
set { ViewState["Names"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var names = new Dictionary<Guid, string>
{
{ Guid.NewGuid(), "John" },
{ Guid.NewGuid(), "Smith" },
{ Guid.NewGuid(), "Arther" },
{ Guid.NewGuid(), "Hari" }
};
ViewState.Add("Names", names);
}
}
protected void btnAddSave_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Names.Add(Guid.NewGuid(), txtNewName.Text);
}
}
in the page load event. I understand that several values have been added to the Dictionary collection, and after that the entire collection is added to the ViewState with the name "Names".
in the event, the btnAddSave_Clickcode Names.Add(Guid.NewGuid(), txtNewName.Text);again adds the name to the collection.
but I just don’t understand how a new value is added to the viewstate.
- private Dictionary<Guid, string> Names, , ViewState. , . , - , ViewState, Names.Add(Guid.NewGuid(), txtNewName.Text);.
, ViewState. , , , ViewState, - collection.i , . . , , , ViewState, . !