The problem is that the SqlDataSource is restored when the page is loaded using the submission provided by the Pager links. This is not to say that it downloads what you installed dynamically. If you must use a stored procedure with parameters, then ASP will save the parameters to ViewState and re-run the selection in SqlDataSource when the page loads.
So what you need to do is tell SqlDataSource what it had for SQL when it loaded correctly.
The easiest way to do this is to save the SQL in the ViewState when installing SelectCommand from SqlDataSource, and then return it again to the Page_Load event and set it back.
For example: suppose you have a TextBox for some criteria and a search button. When a user enters any text into a TextBox and then clicks the Search button, you want him to create some SQL (this, by the way, gives you a huge threat to SQL Injection attacks. Criteria). Then set the SqlDataSource SelectCommand property. This means that you want to save SQL. Then, in the Page_Load event, you want to get it and set the SelectCommand property to this value.
In Click of your button you can save SQL:
Dim sSQL as String sSQL = "SELECT somefields FROM sometable WHERE somefield = '" & Me.txtCriteria.Text & "'" SqlDataSource1.SelectCommand = sSQL ViewState("MySQL") = sSQL
Then in the Page_Load event, you can get SQL and set the SelectCommand property:
Dim sSQL as String If Me.IsPostBack() Then sSQL = ViewState("MySQL") SqlDataSource1.SelectCommand = sSQL End If
source share