2
Basically, I have a table (Gridview) on an aspx page whose data are defined by Templatefields. For example:
<asp:GridView runat="server" ID="grdProdutos">
<Columns>
<asp:TemplateField HeaderText="Produto">
<ItemTemplate>
<asp:Label runat="server" ID="lblProduto" Text='<%# Eval("NomeProduto")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tipo">
<ItemTemplate>
<asp:Label runat="server" ID="lblTipo" Text='<%# Eval("TipoProduto")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preço">
<ItemTemplate>
<asp:Label runat="server" ID="lblPreco" Text='<%# Eval("PrecoProduto")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The methods used in the Behind code are as follows::
RepositorioProdutos banco = new RepositorioProdutos();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CarregarGridView();
}
}
public void CarregarGridView()
{
var produtos = banco.ObterProdutos();
if (produtos != null)
{
grdProdutos.DataSource = produtos;
grdProdutos.DataBind();
}
else
{
throw new Exception("Nenhum produto encontrado.");
}
}
I would like to know how I could implement a product search (looking for a term in any field) and how I could implement an ordering by clicking on the name of the column I would like to sort (as in Windows Explorer), without having to re-consult the bank (that is, without having to access the method banco.ObterProdutos()
again).
Obs.: for the search, I am considering using a textbox that will be used to enter the keyword of the search, and a button that will trigger the search.
Seems like that’s the most practical way to do it. I would like to avoid redoing the query, since if I have a very large amount of data, it will take longer for me to do the search and ordering (not so much the search). The alternative would be to use
Session
orViewState
(which would be an alternative to keep the search and Sort together as well), but keeping a lot of data there would not be very good either.– diogoan
So, keeping in the Viewstate carries the client that can get heavy, keep in Session carries the IIS. I think web projects, different from what would be with desktop, leave these tasks in charge of the SQL server ends up being the less expensive, since the one SGDB is optimized to answer queries.
– iuristona