c# – Dilemma to the popular datalist

Question:

I have a problem I don't even know what to call. But I will try to express this problem here. The thing is, I did a pagination in my datalist with the following code:

private void FillDados()
{
    // Preenche uma lista de objetos para servirem de dataSource do nosso repeater.
    //(Essa lista de objetos pode ser consumida por um dataset por exemplo) 
    RepositorioProdutos Rep_Produtos = new RepositorioProdutos(NHibernateHelper.GetSession());
    IList<HelpDesk.Dominio.Entidades.Produtos> listarprodutos = Rep_Produtos.ObterTodos();
    pagina = Convert.ToInt32(ViewState["pagina"]);
    numeropaginas = Convert.ToInt32(ViewState["numeropaginas"]);

    //Seta a fonte de dados do objeto de paginação como a nossa lista de objetos
    pgds.DataSource = listarprodutos;

    //Permite a paginação do objeto
    pgds.AllowPaging = true;

    //Seta a pagina atual do objeto como sendo nosso ViewState de pagina.
    pgds.CurrentPageIndex = pagina;

    //Seta a quantidade de registros por página
    pgds.PageSize = 32;

    lbtnAnt.Visible = !pgds.IsFirstPage;
    lbtnProx.Visible = !pgds.IsLastPage;

    //Seta a fonte de dados do repeater como o nosso objeto de paginação.

    DataList1.DataSource = pgds;
    DataList1.DataBind();

    ViewState["numeropaginas"] = numeropaginas;
    ViewState["pagina"] = pagina;

}

It's working perfectly. I have two button links : Próximo and Anterior only. The function to go to the next page looks like this:

protected void Proximo(object sender, EventArgs e)
{
    // Vai para a próxima página        
    pagina = Convert.ToInt16(ViewState["pagina"]);
    numeropaginas = Convert.ToInt32(ViewState["numeropaginas"]);

    pagina = pagina + 1;
    if (numeropaginas < pgds.PageCount && pagina > (totalpaginas - numerop - 1))
    {
        numeropaginas = numeropaginas + 1;
    }

    //se forem as ultimas paginas
    else if (pagina >= (pgds.PageCount - ((totalpaginas + 1) / 2)))
    {
        numerop = totalpaginas - (pgds.PageCount - pagina);
    }
    else
    {
        numerop = numerop + 1;
    }
    ViewState["pagina"] = pagina;
    ViewState["numeropaginas"] = numeropaginas;
    if (String.IsNullOrEmpty(txtPesquisar.Text))
    {
        FillDados();
    }
}

As I said above, it works fine. The code above fetches all products in my database and displays them in a datalist . When I click Próximo , it searches for all again and shows the second page of all products.

Now see the code below:

protected void dtlCategoria_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (((LinkButton)e.CommandSource).CommandName == "categoria")
    {
        IDictionary<String, Object> Param = new Dictionary<String, Object>();
        IList<HelpDesk.Dominio.Entidades.Produtos> listarprodutos;
        Int64 IDCategoria = Int64.Parse(e.CommandArgument.ToString());
        Param.Add("Categoria.ID", IDCategoria);
        listarprodutos = Repositorio.ObterPorParametros(Param);

        DataList1.DataSource = listarprodutos;
        DataList1.DataBind();
    }
} 

The code above triggers when clicking on a link that is in a second datalist , it filters by category and shows in the first datalist , which also filters correctly. My problem then was the Próximo and Anterior button link . After I click on category and after generating the filter, when I click on Próximo for example, it goes to the next page but showing all products again, ignoring the filter I did earlier.

I would like that when I click on a category it filters according to it, and if there is a second page in that category it only shows the products from that category, not all of them again as it's happening.

Well, I tried to explain, I hope you understand! I don't know how to make my idea work, can someone help me to come up with a solution?

Answer:

When clicking on a page (causing a postback) you need to maintain the status of the filters you selected earlier as http does not maintain state. There are several ways to do this which involve analyzing the sensitivity of the stored information, you can invoke the other control's methods manually, passing the filter parameters as soon as you click on the page change.

An interesting alternative can be to keep this status in ViewState: https://www.codigofonte.net/dicas/dotnet/253_entendendo-o-que-e-viewstate

It is not good practice for one control to know the implementation of another, or even to mix the control parameters of one control with methods of another. Generally, the environment the control is in passes configuration parameters so that the control knows how to behave. You can do this by specific methods or attributes, it's simple and isolates the logic in specific chunks to configure the behavior of the components.

Scroll to Top