I have a nested 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();

    }

I don't know why but the nested listview (lst_variants) is unique for all products and it corresponds with the result for last product from lst_productsWithDiscounts

So how can I bind the second listview using sqldatasource?

share|improve this question

I found the answer on other post from this site: ASP.NET VB Nested Listviews

So the result is:

<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>

share|improve this answer
    
I did not know that you could nest an SqlDataSource in a ListView ItemTemplate like that. Interesting. Just curious, did you try the solution I provided? I was curious if it would work like that or not. – jadarnel27 Feb 6 '14 at 17:30
    
Unfortunately not. But I don't think it would have worked because in listview it was set DataSourceID="source_discountVariants". Next time when I will have to work with nested listview I will try your solution just for curiosity and I will let you know about the results. – POIR Feb 7 '14 at 8:48

I think you should set your DataSourceID property for the nested ListView in codebehind. It is in a different Naming Container than the SqlDataSource to which it's being bound, so doing it in your markup usually won't work.

Remove the "DataSourceID" property from the lst_variants markup:

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

And then update your code to set that in 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();
}

Note: it would be useful to see the markup for your SqlDataSource(s).

share|improve this answer

This works great for me . I have A Parent listView with a Child listView nested inside. And i created a function that returns a datatable. This function runs a stored precedure from the database

       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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.