c# asp.net listview datapager

c# - ASP.Net: DataPager Control siempre un paso atrás con paginación



listview (6)

Tome el siguiente ejemplo ... una página con un ListView y un DataPager utilizados para paginar los datos de ListView :

Código detrás:

protected void Page_Load(object sender, EventArgs e) { MyList.DataSource = GetSomeList(); MyList.DataBind(); }

Fuente:

<asp:ListView ID="MyList" runat="server"> <% //LayoutTemplate and ItemTemplate removed for the example %> </asp:ListView> <asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"> <Fields> <asp:NumericPagerField /> </Fields> </asp:DataPager>

El problema con el DataPager es que siempre es un paso atrás con el enlace.

Por ejemplo, cuando la página se carga, está en el número de página 1. Luego, cuando hace clic en la página 3, permanece en la página 1 después de la devolución. Luego haga clic en la página 5, y después de la devolución de datos se encuentre en la página 3 ... y luego haga clic en la página 6, y se encuentre en la página 5 ... y así sucesivamente.

¿Por qué la megafonía no funciona como se esperaba?


Solución

El problema se debe a la vinculación que se Page_Load en el evento Page_Load .

Para que esto funcione como se espera, la vinculación debe suceder en el evento OnPreRender del OnPreRender , no en el Page_Load .

Fuente:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10" OnPreRender="ListPager_PreRender"> <Fields> <asp:NumericPagerField /> </Fields> </asp:DataPager>

Código detrás:

protected void Page_Load(object sender, EventArgs e) { //Binding code moved from Page_Load //to the ListView''s PreRender event } protected void ListPager_PreRender(object sender, EventArgs e) { MyList.DataSource = GetSomeList(); MyList.DataBind(); }


¡Te estás perdiendo el evento OnPreRender en el datapager!


Alternativamente, si está creando un control de usuario que solo contenga el ListView, simplemente puede apuntar el controlador de eventos del buscapersonas al método Page_Load , ya que el método Page_Load no ejecuta nada más:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10" OnPreRender="Page_Load">


Me encontré con este mismo problema, pero el enlace cada vez que preparé el datapager no era una opción para mí. En cambio, pude lograr casi lo mismo al enlazar solo cuando ocurrió la búsqueda. Esta solución puede usarse como una alternativa a la solución de prerender de Andreas. Lo siguiente funcionó para mí:

Al adjuntarme al evento PagePropertiesChanged de ListView, pude corregir el problema de la búsqueda sin tener que enlazar cada preentrega del buscapersonas de datos.

NOTA: la mayoría de las propiedades del buscapersonas de datos están configuradas en un archivo skin, por lo que no están en el marcado.

Margen:

<asp:DataPager ID="ListPager" runat="server" PagedControlID="MyList" /> <asp:ListView ID="MyList" runat="server"> <% //LayoutTemplate and ItemTemplate removed for the example %> </asp:ListView>

Código detrás:

protected void Page_Load(object sender, EventArgs e) { MyList.PagePropertiesChanged += new EventHandler(MyList_PagePropertiesChanged); } /// <summary> /// Handles the situation where the page properties have changed. Rebind the data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MyList_PagePropertiesChanged(object sender, EventArgs e) { MyList.DataSource = GetSomeList(); MyList.DataBind(); }


en la carga de la página debes poner el código entre if (! IsPostBack) {}

Solucionará tu problema.


Los siguientes trabajos son perfectos para mí.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ds As DataSet ds = SQLHELPER.ExecuteDataSet(CommandType.StoredProcedure, "sp_Locations") rs.EnableViewState = False rs.DataSource = ds rs.DataBind() End Sub Protected Sub rs_PagePropertiesChanging(ByVal sender As Object, ByVal e As PagePropertiesChangingEventArgs) ''set current page startindex, max rows and rebind to false Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, False) ''rebind List View rs.DataBind() End Sub <asp:ListView ID="rs" runat="server" onpagepropertieschanging="rs_PagePropertiesChanging">