开发者

populate gridview via code-behind or markup DataSource?

开发者 https://www.devze.com 2023-03-16 13:10 出处:网络
The 2 options are: use parameters in aspx file开发者_运维百科s or bind through code-behind. Which is better and why?I recommend using ObjectDataSource because it leads to a cleaner architecture and is

The 2 options are: use parameters in aspx file开发者_运维百科s or bind through code-behind. Which is better and why?


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

In the app_code / business tier:

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
  }
}

In the Web Form:

<asp:ObjectDataSource ID="objProduct" runat="server"
  TypeName="MyNameSpace.BLL.ProductRepository"
  SelectMethod="GetAll"
  EnablePaging="true" 
  SortParameterName="sortOrder"
  SelectCountMethod="GetAllCount"  />


The principle of less code. Place as much as possible in the aspx file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号