3

I am databinding a SqlDatasource to a GridView in 2 different ways.

1) Declaring the SqlDatasource in the .aspx page and setting the DatasourceID property on the GridView to its id

2) Creating the SqlDatasource in the code behind and binding to the datasource property

When I use the declarative method (1), sorting works fine without any extra work on my part, but If I create the SqlDatasource in the code behind and try to sort, I get an exception saying 'The GridView 'myGridView' fired event Sorting which wasn't handled'.

I assume that there is some sort of extra plumbing that occurs when using the first method. Does anyone know what the datasourceid method does that the programmatic one doesn't? I would like to duplicate the logic in the code behind if possible.

1 Answer 1

1

The GridView checks whether it is data bound using the DataSourceId property, and if it's not, then the Sorting event must be handled to do the sorting.

Did you try adding the SqlDatasource which you have created programatically to the control hierarchy of the page (for example into a PlaceHolder), and then binding the GridView to it by using the DataSourceId property? I did not try this, but it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.