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.
精彩评论