如何将datalist与多个数据源绑定

发布于 2024-10-27 19:09:19 字数 143 浏览 3 评论 0原文

我可以将一个数据列表与 2 个数据源绑定吗?场景是我的数据列表绑定在显示产品列表的设计视图上,我想要实现的是,当用户单击侧面菜单过滤器时,数据列表应该显示与该过滤器单击相关的结果,但是我不知道如何禁用以前的数据列表绑定并将其与新的数据列表绑定,具体取决于单击的过滤器选项。

Can I bind a datalist with 2 datasource. The scenario is my data list is bound on the design view which displays a list of products, What I want to achieve is, when a user clicks on a side menu filter, the datalist should show the results with respect to that filter click, but I dunno how to disable the previous binding of datalist and bind it with a new datalist depending on the clicked filter option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-11-03 19:09:19

我建议在设计视图和数据访问层之间构建一个业务对象。在业务层中,您可以传递一个参数来指示过滤器是什么,并采取相应的操作。然后,您只需设置默认过滤器并在过滤器更改时调用 MyGridView.DataBind() 即可。如果这是一个 Web 应用程序,则该业务对象可能位于

app_code\BLL
    Product.cs

Product.cs 中,将包含一个“filter”参数和数据访问层所需的其他参数,例如 orderBy、rowStart、rowEnd。例如,

public class Product
{
  public List<Product> GetAll(string filter, string orderBy, int startRowIndex, int maximumRows)
  {
    List<Product> products = null;
    switch(filter)
    {
      case "option1":
        // to do: products = some data access call
        break;
      case "option2":
        // to do: products = some other data access call
        break;
      default:
        throw new InvalidOperationException("Unexpected filter option.");
    }
    return products;
  }
}

您的 ObjectDataSource 将引用业务层,而不是数据访问层:

<asp:ObjectDataSource
  TypeName="MyAssembly.BLL.Product"
  SelectMethod="GetAll"
  ...
  <SelectParameters>
    <Parameter name="filter"...
    <Paramter name="param1"...

<asp:gridview ...
  DataSourceId="obGridViewSource"

祝你好运!

I suggest building a business object between the design view and the data access tier. In the business tier, you can pass a param indicating what the filter is, and act accordingly. Then you simply need to set a default filter and call MyGridView.DataBind() when the filter changes. If this is a web app, this business object might live in

app_code\BLL
    Product.cs

Product.cs would include a "filter" param and the other params needed for the data access tier, e.g., orderBy, rowStart, rowEnd. For example,

public class Product
{
  public List<Product> GetAll(string filter, string orderBy, int startRowIndex, int maximumRows)
  {
    List<Product> products = null;
    switch(filter)
    {
      case "option1":
        // to do: products = some data access call
        break;
      case "option2":
        // to do: products = some other data access call
        break;
      default:
        throw new InvalidOperationException("Unexpected filter option.");
    }
    return products;
  }
}

Your ObjectDataSource would reference the business tier, rather than the data access tier:

<asp:ObjectDataSource
  TypeName="MyAssembly.BLL.Product"
  SelectMethod="GetAll"
  ...
  <SelectParameters>
    <Parameter name="filter"...
    <Paramter name="param1"...

<asp:gridview ...
  DataSourceId="obGridViewSource"

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文