数据绑定和回发
这是 DataBind 如何工作的一般问题...
我有一个简单的页面,其中有一个 GridView 绑定(在 aspx 代码中)到一个 ObjectDataSource。
我可以查看 ObjectDataSource 调用的 Select() 函数,看看它是在初始加载和每次回发时调用的。 我在回发时发生的一些逻辑会影响 GridView 的数据,并且我想在进行一些更改后在回发中稍后调用 GridView.DataBind() 。
有没有办法防止每次回发时发生自动重新绑定? 这是否意味着我不能为此控件使用 ObjectDataSource?
This is a general how does DataBind work questions...
I have a simple page with a GridView that is bound (in the aspx code) to an ObjectDataSource.
I can look in the Select() function called by the ObjectDataSource to see that it is called on the initial load and on every post back. I have some logic that happens on post backs that will affect the GridView's data, and I want to call GridView.DataBind() later on in the post back, after I've made some changes.
Is there a way to prevent the automatic rebinding that happens on each post back? Does this mean I can't use an ObjectDataSource for this control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是正确的,您正在寻找的细粒度控制是不可能的,并且需要后面的代码。 ASP.NET 的数据源对象只不过是一个**中的痛苦。 您会发现,当您使用它们时,类似的情况会一次又一次地出现。
您会发现的一些问题是:
我已经在后面的代码中进行了所有数据访问,并且没有回头看。
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:
I've taken to doing all data access in the code behind and haven't looked back.
我也与这种自动绑定作斗争,并认为我在这里发布我的解决方案:
我花了一段时间才弄清楚这一点,但现在一切正常。
上下文
我使用ObjectDatasource,因为它自动为我处理Gridview 的所有分页 和排序。 我使用带有 Linq2SQL 的数据层,并使用其 Skip() 和 Take() 方法仅加载填充 GridView 一页所需的数据量。
使用 ObjectDataSource 的 SelectMethod 和 SelectCountMethod
I fought with this automatic binding as well and thought I post my solution here:
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.