I'm new in ASP.Net, and I have a doubt about how things work around the DataBind:
Page_Load: if IsPostBack=false, I load my DataTable var (declared outside the Page_Load) and bind it to the GridView. My idea here was to preserve the DataTable, avoiding new queries to the database.
When I clicked on a different page, the PageIndexChanging was raised, doing this:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataSource = _table; GridView1.DataBind(); }This didn't worked well, since the grid was shown empty. I found in other examples that the data source was completely reloaded, so I put the load code in a function, and changed my PageIndexChanging event:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; LoadData(); GridView1.DataSource = _table; GridView1.DataBind(); }Not it worked like a charm! I understand that my DataTable was cleared between the Page_Load and the PageIndexChanging events, probably because the data table var was not retained by the server (remember I'm a newbie at ASP.NET...), but I would like to understand:
- Why the server does not retains the variable?
- There is a way to bypass this situation in order to avoid multiple server data requests? What are the pros & cons in the bypass?
Thanks a lot!