【问题标题】:Datasource needed to link supplier with products for each supplier将供应商与每个供应商的产品联系起来所需的数据源
【发布时间】:2012-09-28 06:47:02
【问题描述】:

我们希望在 GridView 中显示供应商以及所显示的每个产品的项目符号列表。

例如:

Supplier One               Product A
                           Product B

Supplier Two               Product A
                           Product B
                           Product C

这是 GridView 的样子:

        <asp:GridView 
            ID="GridView1" 
            runat="server" 
            AutoGenerateColumns="False" 
            CssClass="DataWebControlStyle">

            <HeaderStyle CssClass="HeaderStyle" />

            <AlternatingRowStyle CssClass="AlternatingRowStyle" />

            <Columns>
                <asp:BoundField 
                    DataField="CompanyName" 
                    HeaderText="Supplier" />

                <asp:TemplateField HeaderText="Products">
                    <ItemTemplate>
                        <asp:BulletedList 
                            ID="BulletedList1" 
                            runat="server" 
                            DataSource='<%#  %>'
                            DataTextField="ProductName">
                        </asp:BulletedList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

这是将数据加载到 GridView 中的代码隐藏文件:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim suppliersAdapter As New SuppliersTableAdapter

    GridView1.DataSource = suppliersAdapter.GetSuppliers()
    GridView1.DataBind()
End Sub

您能告诉我们在 asp:BulletedList DataSouce 中放置什么以便产品也可以显示吗?

* 更新 *

感谢大家的帮助。我删除了 DataSource='' 行,根据您在代码隐藏文件中的帮助,这里是现在额外的工作编码。

由于我正在学习 ASP.Net,因此我在代码中添加了很多 cmets,以帮助自己了解发生了什么以及它运行的原因。如果我在 cmets 中有错误,请调整 cmets。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' TableAdapter object.
    ' Provide communication between this application and the database.
    '-----------------------------------------------------------------
    Dim suppliersAdapter As New SuppliersTableAdapter

    ' Get the data from the TableAdapter into the GridView.
    '------------------------------------------------------
    GridView1.DataSource = suppliersAdapter.GetSuppliers()

    ' Display the result set from the TableAdapter in the GridView.
    '--------------------------------------------------------------
    GridView1.DataBind()
End Sub

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    ' A GridView has DataRows, EmptyDataRows, Footers, Headers, Pagers, and Separators.
    ' The DataSource will be assigned to the BulletedList in this GridView only when the RowType is a DataRow.
    ' This will make sure the "Object reference not set to an instance of an object" error will not be thrown.
    '---------------------------------------------------------------------------------------------------------
    If e.Row.RowType = DataControlRowType.DataRow Then

        ' TableAdapter object.
        ' Provide communication between this application and the database.
        '-----------------------------------------------------------------
        Dim productsAdapter As New ProductsTableAdapter

        ' BulletedList object.
        ' This object is created from the BulletedList control in the aspx file for GridView 
        ' so it can be used to assign a DataSource to the BulletedList.
        '------------------------------------------------------------------------------------
        Dim BulletedList1 As BulletedList = DirectCast(e.Row.FindControl("BulletedList1"), BulletedList)

        ' Get the SupplierID into a variable for use as a parameter for the GetProductsBySupplierID method of the TableAdapter.
        '----------------------------------------------------------------------------------------------------------------------
        Dim supplier = DataBinder.Eval(e.Row.DataItem, "SupplierID")

        ' Get the data from the TableAdapter into the GridView.
        '------------------------------------------------------
        BulletedList1.DataSource = productsAdapter.GetProductsBySupplierID(supplier)

        ' Display the result set from the TableAdapter in the GridView.
        '--------------------------------------------------------------
        BulletedList1.DataBind()
    End If
End Sub

【问题讨论】:

    标签: asp.net vb.net datasource code-behind bulletedlist


    【解决方案1】:

    每个列表都有不同的数据源。

    您可以通过在网格的RowDataBound 事件上的BulletedList 上设置数据源来实现您的目标。

    附加RowDataBound 事件处理程序如下:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType==DataControlRowType.DataRow) 
       {       
           BulletedList BulletedList1 = (BulletedList)e.Row.FindControl("BulletedList1"); 
           var supplier = DataBinder.Eval(e.Row.DataItem, "CompanyName");
           BulletedList1.DataSource = GetProductListForSupplier(supplier);
           BulletedList1.DataBind();
       }
    }
    

    【讨论】:

    • 感谢大家的帮助。 :-) 我根据您的帮助更新了我的帖子。
    【解决方案2】:

    试试这个

      protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          string supplierName = e.Row.Cells[0].Text;
          //OR assuming you stored the SupplierID in hidden view - this can also be retrieved from GridView DataKeys value for each row
          string supplierID = ((HiddenField) e.Row.FindControl("hiddenFieldSupplierID")).Value;
    
          DataTable products = getProductsBySupplier(supplierID); //or by SupplierName
          BulletedList BulletedList1 = ((BulletedList) e.Row.FindControl("BulletedList1"))
          BulletedList1.DataSource = products;
          BulletedList1.DataBind()     
    
        }
    
      }
    

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2021-07-30
      • 2022-06-11
      • 2022-11-07
      相关资源
      最近更新 更多