通过代码隐藏或标记DataSource填充gridview?

时间:2011-06-29 20:28:50

标签: asp.net webforms

这两个选项是:在aspx文件中使用参数或通过代码隐藏绑定。哪个更好,为什么?

2 个答案:

答案 0 :(得分:2)

我建议使用ObjectDataSource,因为它可以提供更清晰的架构,并且更容易处理事件,例如排序和分页。否则,你的控制必须专门处理这样的事件,我发现这是一个痛苦的问题。我总是创建一个业务层,并让我的Get()方法使用如下所示的签名。我的这种设计模型来自本书,我认为这是一个很棒的 Web Forms 资源:

http://www.amazon.com/ASP-NET-2-0-Website-Programming-Programmer/dp/0764584642

在app_code / business层中:

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"  />

答案 1 :(得分:-1)

代码少的原则。尽可能放在aspx文件中。

相关问题