Ho un GridView che ha un DataSourceID che punta a un ObjectDataSource. ObjectDataSource punta a un metodo che restituisce un LINQ IQueryable utilizzando le proprietà TypeName, SelectMethod e SelectCountMethod del controllo ObjectDataSource. Quello che succede è che i dati vengono caricati correttamente in anticipo. Tuttavia, nel postback, se rimuovo le righe da GridView e provo a rebind usando il GridView.DataBind() esplicito, non funziona. So che LINQ sta restituendo il conteggio delle righe corretto e tale perché ho chiamato il metodo count e restituisce il conteggio delle righe corretto. Ecco un rapido esempio:GridView non riconfigura correttamente dopo il postback
<asp:GridView ID="TestGridView" runat="server" PageSize="20"
AutoGenerateColumns="false" AllowPaging="true"
AllowSorting="false" DataSourceID="TestDataSource">
<Columns>
...
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="TestDataSource" runat="server"
EnablePaging="true" SelectCountMethod="GetDetailCount"
SelectMethod="GetDetails" TypeName="MyApp.PageClass" />
Ho provato ad aggiungere un pulsante e aggiungendo il TestGridView.DataBind(); metodo per quello Ho provato ad aggiungerlo all'evento Page_PreRender. Non importa quello che provo, non funziona.
Come suggerito da qualcuno di seguito, ho provato a spostarlo anche su Page_Load e non andare. Ecco un esempio di massima del mio codice:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Set "initial" query parameters, then ...
BindData();
}
}
private void BindData()
{
// EDITED: Removed the code below since I'm not looking to delete the
// rows from the database, but rather get the GridView to rebind
// which its not.
////Remove all current rows from the GridView
//int colCount = TestGridView.Rows.Count;
//for (int x = 1; x <= colCount; x++)
//{
// TestGridView.DeleteRow(x);
//}
// Bind GridView to the ObjectDataSource
TestGridView.DataBind();
}
protected void RegenerateImageButton_Click(object sender, ImageClickEventArgs e)
{
// Set "updated" query parameters, then ...
BindData();
}
Modificato nuovamente sopra. –