为什么是必要的DataBind()方法? [英] Why is the DataBind() method necessary?

查看:212
本文介绍了为什么是必要的DataBind()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,我猜。

在很长一段时间,我一直盲目地跟着(据说)常见的模式时,编程数据绑定我的ASP.NET控件。即:

For a long time I've blindly followed a (supposedly) common pattern when programmatically databinding my ASP.NET controls. Namely:

gridView1.DataSource = someList;
gridView1.DataBind();

不过,如果我被我的设置GridView控件绑定到一个数据源的控制的通过DataSourceID属性,调用数据绑定()是不必要的。即:

However, if I were setting my GridView to bind to a DataSource control via the DataSourceID property, the call to DataBind() is unnecessary. Namely:

gridView1.DataSourceID = LinqDataSource1;

是足够的。

此外,如果您尝试设置在ASPX标记的DataSource属性,您可获赠以下内容:

Furthermore, if you try to set the DataSource property in ASPX markup, you are greeted with the following:

您不能声明设定DataSource属性。

You cannot set the DataSource property declaratively.

我认为这些是相关的,但我还是难倒,为什么的DataBind()是必要的。 DataSource和DataSourceID的之间的差异是次要的 - 我能理解一些神奇那里正在发生。真正的问题是的为什么不将DataSource属性格式二传手原因自动绑定?是否有任何场景中,我们要设置数据源,但不能结合呢?

I assume these are related, but I am still stumped as to why DataBind() is necessary. The difference between DataSource and DataSourceID is secondary - I can understand some magic taking place there. The real question is why doesn't the DataSource propery setter cause databinding automatically? Are there any scenarios in which we want to set the DataSource but not bind to it?

推荐答案

在ASP.Net,它有一定的可用数据,并准备在该页面生命周期的特定点往往很重要,而不是之前。例如,您可能需要绑定到一个下拉列表年初以后允许清单上设置选定的索引。或者你可能要稍等一下绑定的大电网,以减少时间,你认为连接主动/保持内存中的数据量。

In ASP.Net, it's often important to have certain data available and ready at certain points in the page life cycle, and not before. For example, you may need to bind to a drop down list early to allow setting the selected index on that list later. Or you might want to wait a bit to bind that large grid to reduce the amount of time you hold that connection active/keep the data in memory.

有你明确地调用.DataBind()方法,使之成为可能。

Having you explicity call the .DataBind() method makes this possible.

这篇关于为什么是必要的DataBind()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆