Well, I have a small problem with asp.net webforms . And I gave a cool pack.
I know the solution to this may be a bit simple, but I'm confused.
Follow the code:
public partial class Default : System.Web.UI.Page
{
List<Pessoa> lista;
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) return;
if (lista == null)
lista = new List<Pessoa>();
}
protected void btnEnviar_Click(object sender, EventArgs e) {
Pessoa p = new Pessoa();
p.Id = lista.Count + 1;
p.Nome = "João";
p.Sobrenome = "Silva";
p.Idade = 20;
lista.Add(p);
CarregarGrid();
}
private void CarregarGrid() {
gvDados.DataSource = lista;
gvDados.DataBind();
}
}
When I click the button, I want to add a new person to the Grid, but it overrides, always on the pageload page the list is 'null'. It always adds:
1 - joao - silva.
How can I do this by keeping the current information? Whenever I click the button I want to always add one more, as an example below:
1 - joao - silva. 2 - joao - silva. 3 - joao - silva ....