通过代码隐藏或标记数据源填充 gridview?

populate gridview via code-behind or markup DataSource?(通过代码隐藏或标记数据源填充 gridview?)
本文介绍了通过代码隐藏或标记数据源填充 gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个选项是:使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)