DataBind and Postback

落爺英雄遲暮 提交于 2019-11-29 10:37:00

You're correct in that the fine grained control you're looking for is not possible and requires the code behind. ASP.NET's data source objects are nothing but a pain in the a**. You'll find that as you use them you'll get situations like this cropping up again and again.

Some of the problems you'll find are:

  • Not strongly typed
  • Inflexible (as you've noted)
  • Muddy up the presentation code

I've taken to doing all data access in the code behind and haven't looked back.

I fought with this automatic binding as well and thought I post my solution here:

  1. remove the "DataSourceID" from the ASPX page, when its not set, there is no automatic binding
  2. set the DataSourceID in the CodeBehind only when DataBinding is needed: myGridView.DataSourceID = "MyDataSource";
  3. do not call myGridView.DataBind() explicitly, databinding happens automatically at PreRender

Took me a while to figure this out, but now wverything works fine.

Context

I use the ObjectDatasource because it handels all the paging and sorting of the Gridview automatically for me. I am using a data layer with Linq2SQL and use its Skip() and Take() methods to load only the amount of data needed to populate one page of the GridView.

Using the SelectMethod and SelectCountMethod of the ObjectDataSource

Yes. If you want that kind of control over when the databinding happens you need to do it in the code behind.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!