问题描述
这两个选项是:使用 aspx 文件中的参数或通过代码隐藏进行绑定.哪个更好,为什么?
The 2 options are: use parameters in aspx files or bind through code-behind. Which is better and why?
推荐答案
我推荐使用 ObjectDataSource,因为它可以带来更简洁的架构,并且更容易处理事件,例如排序和分页.否则,您的控件必须专门处理此类事件,我觉得这令人头疼.我总是创建一个业务层,并让我的 Get() 方法使用如下所示的签名.我的这种设计模型来自这本书,我认为这是一个很好的网络表单资源:
I recommend using ObjectDataSource because it leads to a cleaner architecture and is much easier to deal with events, e.g., sorting and paging. Otherwise, your control must specifically handle such events, which I find to be a pain in the neck. I always create a business tier and have my Get() methods use signatures like those shown below. My model for this kind of design comes from this book, which I think is a great Web Forms resource:
http://www.amazon.com/ASP-NET-2-0-Website-Programming-Programmer/dp/0764584642
在 app_code/业务层:
public class ProductRepository
{
public List<Product> GetAll(/* params here */ string sortOrder, string orderBy, int startRowIndex, int maximumRows)
{
// call data access tier for Product entities
}
public int GetAllCount(/* params here */ )
{
// call data access tier for count of Product entities
}
}
在网络表单中:
<asp:ObjectDataSource ID="objProduct" runat="server"
TypeName="MyNameSpace.BLL.ProductRepository"
SelectMethod="GetAll"
EnablePaging="true"
SortParameterName="sortOrder"
SelectCountMethod="GetAllCount" />
这篇关于通过代码隐藏或标记数据源填充 gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!