嵌套ListView使用两个不同的sqldatasources

时间:2014-02-04 14:33:21

标签: c# asp.net listview sqldatasource nested-lists

我有一个嵌套的listview

<asp:ListView ID="lst_productsWithDiscounts" DataKeyNames="ProductID" runat="server" DataSourceID="source_discountProducts" ItemPlaceholderID="plc_product" OnItemDataBound="lst_productsWithDiscounts_ItemDataBound">
<LayoutTemplate>
    <table>
       <tr>
            <td>Name</td>
            <td>Price</td>
            <td>TVA</td>
            <td>Discount</td>
            <td>Price</td>
            <td>New discount</td>
            <td>New price</td>
       </tr>
       <tr id="plc_product" runat="server">
       </tr>
    </table>


</LayoutTemplate>
<ItemTemplate>
  <tr>
        <td><%# Eval("ProductCode").ToString()+" / "+ Eval("Name").ToString() %></td>
        <td><%# Eval("SellPrice").ToString() %></td>
        <td><%# Eval("TVA").ToString() %></td>
        <td><%# Eval("Discount").ToString() %></td>
        <td><%# BC_Common.Util.Calc(double.Parse(Eval("SellPrice").ToString()),double.Parse(Eval("Discount").ToString()),Convert.ToInt32(Eval("TVA").ToString()),1).ToString() %></td>
        <td><%# Eval("NewDiscount").ToString() %></td>
        <td><%# BC_Common.Util.Calc(double.Parse(Eval("SellPrice").ToString()), double.Parse(Eval("NewDiscount").ToString()), Convert.ToInt32(Eval("TVA").ToString()), 1).ToString()%></td>
  </tr>
  <tr>
    <td colspan="7">
        <asp:ListView ID="lst_variants" runat="server"  DataSourceID="source_discountVariants" ItemPlaceholderID="plc_variant">
            <LayoutTemplate>
            <table>
                <tr>
                    <td>Variant name</td>
                    <td></td>
                    <td>Stock</td>
                    <td>Reserved</td>
                </tr>
                <tr id="plc_variant" runat="server"></tr>
             </table>
            </LayoutTemplate>
            <ItemTemplate>
                <td><%# Eval("Nume").ToString() %></td>
                <td></td>
                <td><%# Eval("Stock").ToString() %></td>
                <td><%# Eval("Reserved").ToString() %></td>
            </ItemTemplate>
        </asp:ListView>
    </td>
  </tr>
</ItemTemplate>

 protected void lst_productsWithDiscounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        source_discountVariants.SelectParameters["ProductID"].DefaultValue = lst_productsWithDiscounts.DataKeys[e.Item.DataItemIndex]["ProductID"].ToString();

        ListView lst_variants = e.Item.FindControl("lst_variants") as ListView;
        lst_variants.DataBind();

    }

我不知道为什么,但嵌套列表视图( lst_variants )对于所有产品都是唯一的,它与 lst_productsWithDiscounts

中最后一个产品的结果相对应

那么如何使用sqldatasource绑定第二个listview?

3 个答案:

答案 0 :(得分:1)

我在本网站的其他帖子中找到了答案:ASP.NET VB Nested Listviews

结果是:

<asp:ListView ID="lst_productsWithDiscounts" DataKeyNames="ProductID" runat="server" DataSourceID="source_discountProducts" ItemPlaceholderID="plc_product" >
<LayoutTemplate>
    <table>
       <tr>
            <td>Name</td>
            <td>Price</td>
            <td>TVA</td>
            <td>Discount</td>
            <td>Price</td>
            <td>New discount</td>
            <td>New price</td>
       </tr>
       <tr id="plc_product" runat="server">
       </tr>
    </table>


</LayoutTemplate>
<ItemTemplate>
  <tr>
        <td><asp:Label ID="lbl_id" runat="server" Text='<%# Eval("ProductID").ToString() %>'></asp:Label>  <%# Eval("ProductCode").ToString()+" / "+ Eval("Name").ToString() %></td>
        <td><%# Eval("SellPrice").ToString() %></td>
        <td><%# Eval("TVA").ToString() %></td>
        <td><%# Eval("Discount").ToString() %></td>
        <td><%# BC_Common.Util.Calc(double.Parse(Eval("SellPrice").ToString()),double.Parse(Eval("Discount").ToString()),Convert.ToInt32(Eval("TVA").ToString()),1).ToString() %></td>
        <td><%# Eval("NewDiscount").ToString() %></td>
        <td><%# BC_Common.Util.Calc(double.Parse(Eval("SellPrice").ToString()), double.Parse(Eval("NewDiscount").ToString()), Convert.ToInt32(Eval("TVA").ToString()), 1).ToString()%></td>
  </tr>
  <tr>
    <td colspan="7">
        <asp:ListView ID="lst_variants" runat="server"  DataSourceID="source_discountVariants" ItemPlaceholderID="plc_variant">
            <LayoutTemplate>
            <table>
                <tr>
                    <td>Variant name</td>
                    <td></td>
                    <td>Stock</td>
                    <td>Reserved</td>
                </tr>
                <tr id="plc_variant" runat="server"></tr>
             </table>
            </LayoutTemplate>
            <ItemTemplate>
            <tr>
                <td><%# Eval("Nume").ToString() %></td>
                <td></td>
                <td><%# Eval("Stock").ToString() %></td>
                <td><%# Eval("Reserved").ToString() %></td>
                </tr>
            </ItemTemplate>
            <EmptyDataTemplate>
            empty data template
            </EmptyDataTemplate>
            <EmptyItemTemplate>
                empty item
            </EmptyItemTemplate>
        </asp:ListView>

        <asp:SqlDataSource ID="source_discountVariants" runat="server" ConnectionString='<%$ ConnectionStrings: DatabaseConnectionString %>'

            SelectCommand="SELECT * FROM ProductVariant V INNER JOIN VariantForProClientDiscount D ON D.ID_Variant=V.ID INNER JOIN Product P ON P.ID=V.ProductID WHERE V.ProductID=@ProductID"
            SelectCommandType="Text">
      <SelectParameters>
         <asp:ControlParameter Name="ProductID" ControlID="lbl_id"  PropertyName="Text" DbType="Int32" />
     </SelectParameters>
    </asp:SqlDataSource>

    </td>
  </tr>
</ItemTemplate>

答案 1 :(得分:0)

我认为你应该在代码隐藏中为嵌套的ListView设置你的DataSourceID属性。它与绑定它的SqlDataSource位于不同的命名容器中,因此在标记中执行它通常不起作用。

从lst_variants标记中删除“DataSourceID”属性:

<asp:ListView ID="lst_variants" runat="server" ItemPlaceholderID="plc_variant">
        <LayoutTemplate>

然后更新代码以在codebehind中设置:

protected void lst_productsWithDiscounts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    source_discountVariants.SelectParameters["ProductID"].DefaultValue = lst_productsWithDiscounts.DataKeys[e.Item.DataItemIndex]["ProductID"].ToString();

    ListView lst_variants = e.Item.FindControl("lst_variants") as ListView;
    lst_variants.DataSourceID = source_discountVariants;
    lst_variants.DataBind();
}

注意:查看SqlDataSource的标记会很有用。

答案 2 :(得分:0)

这对我很有用。我有一个父列表视图与嵌套在里面的子列表。我创建了一个返回数据表的函数。此函数运行来自数据库的存储过程

       Protected Sub lvParent_ItemDataBound(sender As Object, e As
    ListViewItemEventArgs)
            Dim dtChild As DataTable
             dtChild = Child_SEL(New Guid(lvParent.DataKeys(e.Item.DataItemIndex)("ID").ToString()))
             Dim lvChild As ListView = TryCast(e.Item.FindControl("lvChild"), ListView)
             lvChild.DataSource = dtChild
             lvChild.DataBind()
         End Sub



    Public Function Child_SEL(ParentID As Object) As DataTable
            Dim conn As SqlConnection = New SqlConnection(ConnectionString)
            Dim cmd As SqlCommand = conn.CreateCommand()
            Dim dt As DataTable = Nothing
            Dim ds As New DataSet
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "Child_SEL" 
Dim param_ParentID As SqlParameter = cmd.Parameters.Add("@ParentID ", SqlDbType.UniqueIdentifier)
            param_ParentID.Value = ParentID
            Try
                conn.Open()
                Dim da As New SqlDataAdapter(cmd)
                da.Fill(ds, "x")
                dt = ds.Tables("x")
            Catch e As Exception
                Error_INS(Today, Page.ToString, MethodBase.GetCurrentMethod().Name.ToString(), e.Message.ToString)
                HttpContext.Current.Response.Redirect("~/Secure/Error.aspx")
            Finally
                conn.Close()
            End Try
            Return dt
        End Function

    'Stored Procedure Code
    ALTER PROC [dbo].[Child_SEL]
    @ParentID UNIQUEIDENTIFIER                    
    AS
    BEGIN
    SELECT Bla,Bla,Bla
    FROM Child WHERE ParentID = @ParentID 
    END