Click here to Skip to main content
15,888,219 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
i am developing a e-commerce web application.
I have a web page with a grid view content products , paging=true .
When i order ,for example, order by price, it works fine but only in the first page because when i change in second page, it lost the order by command and doesn't order by.

How can i fix this ?

What I have tried:

Hi all,
i am developing a e-commerce web application.
I have a web page with a grid view content products , paging=true .
When i order ,for example, order by price, it works fine but only in the first page because when i change in second page, it lost the order by command and doesn't order by.

How can i fix this ?
Posted
Updated 30-Apr-17 0:13am
Comments
F-ES Sitecore 28-Apr-17 10:16am    
You have to remember the data must be ordered between page requests. As we don't know how you are navigating between pages we can't give you specific help. If you are using links like data.aspx?page=2 then add the ordering as a param to that link too so that when you bind your data it is in the correct order. If you're using linkbuttons you can store the ordering in the viewstate. We have no idea how you are implementing anything so can't offer much help.
EmanuelPirovano 28-Apr-17 10:19am    
The grid view is host in the same page.
The order by is done by a button with an sql command.
F-ES Sitecore 28-Apr-17 10:25am    
You'll need to update your question to supply the relevant code, we still have no idea how you are implementing things.
EmanuelPirovano 28-Apr-17 10:34am    
SqlDataSource1.SelectCommand = "SELECT ID, NomeProdotto, DescrizioneProdotto, PrezzoProdotto, Data , Quantità FROM aProdotti WHERE NomeProdotto like '%" + TextBox1.Text + "%' OR DescrizioneProdotto like '%" + TextBox1.Text + "%' ORDER BY PrezzoProdotto ASC"

This is the sql search-order by function and it works only in the first page of grid view, when i switch into 2 page, it lost the sql command and set as random the items.
CHill60 28-Apr-17 11:04am    
As F-ES Sitecore said, we have no idea how your are navigating between pages. Use the Improve question link to provide the code that does the navigation.
You should also use Parameterized queries not string concatenation to create your SQL command - see Query Parameterization Cheat Sheet - OWASP[^]
I also don't see any evidence of paging in your SQL query

1 solution

EmanuelPirovano[^] wrote in the comment to the question:
C#
SqlDataSource1.SelectCommand = "SELECT ID, NomeProdotto, DescrizioneProdotto, PrezzoProdotto, Data , Quantità FROM aProdotti WHERE NomeProdotto like '%" + TextBox1.Text + "%' OR DescrizioneProdotto like '%" + TextBox1.Text + "%' ORDER BY PrezzoProdotto ASC"


This is the sql search-order by function and it works only in the first page of grid view, when i switch into 2 page, it lost the sql command and set as random the items.


As i mentioned, your code is SQLInjection[^] vulnerable! You have to use parameterized queries instead!
See:
How To: Protect From SQL Injection in ASP.NET[^]
Writing Secure Dynamic SQL in SQL Server[^]
Walkthrough: Using Only Stored Procedures (C#)[^]

I have no idea what you mean by saying: "order by in a grid view with paging". So, i can provide links to MSDN documentation, eg.:
Tutorial 25: Efficiently Paging Through Large Amounts of Data[^]
Paging in a DetailsView Web Server Control[^]
Paging in a GridView Web Server Control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900