Dilemma to the popular datalist

Asked

Viewed 147 times

3

I have a problem that I don’t even know what to call. But I will try to express this problem here. Here’s the thing, I made 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 of going 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 well. The above code searches all products in my database and shows in a datalist. When I click on Próximo, it searches all again and shows the second page of all products.

Now look at 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 above code fires by 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 button link of Próximo and Anterior. After I click on category and after generating the filter, by clicking on the Próximo for example, it goes to the next page but showing all products again, ignoring the filter I made earlier.

I would like that when I click on a category it filters according to the same, and if there is a second page of that category it shows only the products of that category, and does not show everyone again how is happening.

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

  • ItemCommand is not called when you click "Next" or "Previous". How about isolating the filter in a function and placing a call to this function where it is already known that events will be called?

  • Vote today! Vote tomorrow! Vote always! Vote consciously! Your vote is very important to our community, contribute to us, and help make Stack Overflow in Portuguese (Sopt) bigger and bigger. You can learn more at: Vote early, vote often

1 answer

1

By clicking on a page (provoking a postback) you need to maintain the status of the filters you selected before since http does not maintain status. There are several ways to do this that involve analyzing the sensitivity of the stored information, you can invoke the methods of the other control manually by passing the filter parameters as soon as you click on the page change.

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

It is not good practice for a control to know the implementation of another, nor even to mix the control parameters of one control into methods of another. Generally, the environment where the control is located passes the configuration parameters so that the control knows how to behave. You can do this by specific methods or attributes, it is simple and isolates logic in specific parts for configuration of component behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.