I have that code in my page :
public partial class Utworz : System.Web.UI.Page
{
private List<Codes> Codes;
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Codes;
Codes = new List<Codes>();
}
protected void btn_AddKodManually_Click(object sender, EventArgs e)
{
Codes.Add(new Codes(code: this.TextBox1.Text, codeType: this.DropDownList1.SelectedValue));
this.GridView1.DataBind();
}
}
My page contains UpdatePanel in which I have form. I type data in this form and add to GridView.
and problem is that every time when I call Click method my Page was create new, and my variable Code also. Why this variable not save his state ?
Problem #1: The Codes variable is getting set to a new object every time the page loads. It will never have more than one code.
Problem #2: There is nothing here to hold the value of the Codes list from page view to page view. You need to store it somehow and retrieve it every time you want to rebind. For example, create a property called Codes and store the value in the viewstate. After adding a new code, rebind the grid.
I understand that you might expect the grid to hold it’s state, but you’re rebinding with a new object every time. Someone else might chime in here, but you may be able to restore the Codes object by calling:
Codes = this.GridView1.Datasource