【发布时间】:2011-06-29 20:28:49
【问题描述】:
这 2 个选项是:在 aspx 文件中使用参数或通过代码隐藏进行绑定。哪个更好,为什么?
【问题讨论】:
这 2 个选项是:在 aspx 文件中使用参数或通过代码隐藏进行绑定。哪个更好,为什么?
【问题讨论】:
我推荐使用 ObjectDataSource,因为它可以带来更简洁的架构,并且更容易处理事件,例如排序和分页。否则,您的控件必须专门处理此类事件,我觉得这是一件令人头疼的事情。我总是创建一个业务层并让我的 Get() 方法使用如下所示的签名。我的这种设计模型来自这本书,我认为这是一个很棒的Web Forms资源:
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" />
【讨论】:
少代码原则。尽量放在aspx文件中。
【讨论】: