Wednesday, 17 April 2013

Repeater Inside a Repeater



Repeater inside a repeater(Asp.net and c# coding)
Asp.net code
                     <tr id="rptRow" visible="false" runat="server">
                        <td colspan="3">
                            <table id="Table1" width="100%" cellpadding="5" cellspacing="0" border=" 1px solid green">
                                <asp:Repeater ID="rptCheckVoucher" runat="server" OnItemDataBound="rptCheckVoucher_ItemDataBound">
                                    <HeaderTemplate>
                                        <tr style="background-color: #0D2D37; color: Navy;">
                                        <%--you can take the 'th' value according to your own condition, this is an example--%>
                                            <th align="left" style="width: 50px;">
                                                Date
                                            </th>
                                            <th align="left" style="width: 100px;">
                                                Voucher No.
                                            </th>
                                            <th align="center" style="width: 150px;" colspan="3">
                                                Voucher Posting Details
                                            </th>
                                        </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr>
                                            <td>
                                                <asp:HiddenField ID="hdnVoucherId" runat="server" Value='<%# Eval("Id")%>' />
                                                <%# Eval("VoucherDate","{0:dd/MMM/yyyy}")%>
                                            </td>
                                            <td>
                                                <%# Eval("VoucherNo")%>
                                            </td>
                                            <td colspan="3">
                                                <table width="100%" cellpadding="5" cellspacing="0">
                                                    <tr>
                                                        <asp:Repeater ID="rptCheckVoucherDetail" runat="server" OnItemDataBound="rptCheckVoucherDetail_ItemDataBound">
                                                            <ItemTemplate>
                                                                <tr style="background-color: Gray; color: White;">
                                                                 <%--you can take the 'td' value according to your own condition, this is an example--%>
                                                                    <td style="border: 0px; width: 120px;">
                                                                        <%# Eval("Ledger.LedgerName")%>
                                                                    </td>
                                                                    <td style="border: 0px">
                                                                        <%# Eval("DebitAmount", "{0:0.00}")%>
                                                                    </td>
                                                                    <td style="border: 0px">
                                                                        <%# Eval("CreditAmount", "{0:0.00}")%>
                                                                    </td>
                                                                </tr>
                                                            </ItemTemplate>
                                                            <HeaderTemplate>
                                                                <th align="left" style="width: 100px;">
                                                                    Account Head
                                                                </th>
                                                                <th align="left" style="width: 100px;">
                                                                    Debit
                                                                </th>
                                                                <th align="left" style="width: 100px;">
                                                                    Credit
                                                                </th>
                                                            </HeaderTemplate>
                                                            <FooterTemplate>
                                                                <tr>
                                                                    <th align="left" style="width: 100px;">
                                                                        Total
                                                                    </th>
                                                                    <th align="left" style="width: 100px;">
                                                                        <asp:Label ID="lblDebit" runat="server">0.00
                                                                        </asp:Label>
                                                                    </th>
                                                                    <th align="left" style="width: 100px;">
                                                                        <asp:Label ID="lblCredit" runat="server">0.00
                                                                        </asp:Label>
                                                                    </th>
                                                                </tr>
                                                            </FooterTemplate>
                                                        </asp:Repeater>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <tr>
                                            <th align="left" style="width: 50px;">
                                            </th>
                                            <th align="left" style="width: 100px;">
                                            </th>
                                            <th align="left" style="width: 120px; border-right-width: 0px;">
                                                <asp:Label ID="lblTotal" runat="server" Text="Total" ForeColor="Red">
                                                </asp:Label>
                                            </th>
                                            <th align="left" style="width: 100px; border-left-width: 0px; border-right-width: 0px;">
                                                <asp:Label ID="lblTotalDebitAmount" runat="server" ForeColor="Red">0.00
                                                </asp:Label>
                                            </th>
                                            <th align="left" style="width: 100px; border-left-width: 0px">
                                                <asp:Label ID="lblTotalCreditAmount" runat="server" ForeColor="Red">0.00
                                                </asp:Label>
                                            </th>
                                        </tr>
                                    </FooterTemplate>
                                </asp:Repeater>
                            </table>
                        </td>
                    </tr>

code behind(in c#)
 protected void lbtnViewDetails_Click(object sender, EventArgs e)
    {
        //if (ddlVoucherNumber.SelectedIndex <= 0 || txtFromDate.Text == string.Empty || txtToDate.Text == string.Empty) return;
        try
        {
            rptRow.Visible = true;
            rptCheckVoucher.DataSource = null;//This is the first repeater. 2nd repeater you can find in the data bound event of 1st repeater.
            rptCheckVoucher.DataBind();           

            if (vouchers != null)//Here 'vouchers' means its a collection from database, you can use your own data collection
            {
                if (vouchers.Count == 0)
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No Records Found')", true);
                else
                {
                    rptCheckVoucher.DataSource = vouchers;
                    rptCheckVoucher.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            divError.Visible = true;
            lblError.Text = ex.Message;
        }
    }

 protected void rptCheckVoucher_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            Repeater rptCheckVoucherDetail = (Repeater)e.Item.FindControl("rptCheckVoucherDetail");
            HiddenField hdnVoucherId = (HiddenField)e.Item.FindControl("hdnVoucherId");
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IList<VoucherDetail> voucherDetails = (vouchers.SelectMany(voucher => voucher.VoucherDetails)).Where(voucher => voucher.VoucherId == hdnVoucherId.Value.ToGuid()).ToList();

                rptCheckVoucherDetail.DataSource = voucherDetails;//'voucherDetails' is the collection example, you can use your own collection.
                rptCheckVoucherDetail.DataBind();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                ((Label)e.Item.FindControl("lblTotalDebitAmount")).Text = (vouchers.SelectMany(voucher => voucher.VoucherDetails).Sum(totalDebit => totalDebit.DebitAmount).ToString()); // Debit Amount;
                ((Label)e.Item.FindControl("lblTotalCreditAmount")).Text = (vouchers.SelectMany(voucher => voucher.VoucherDetails).Sum(totalCredit => totalCredit.CreditAmount).ToString());//Credit Amount;   
            }
        }
        catch (Exception ex)
        {           
            throw ex;
        }
    }
 protected void rptCheckVoucherDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)//2nd repeater some functionality inside data bound.
    {
        HiddenField hdnVoucherId = (HiddenField)e.Item.Parent.Parent.FindControl("hdnVoucherId");
        if (e.Item.ItemType == ListItemType.Footer)
        {
            ((Label)e.Item.FindControl("lblDebit")).Text = ((vouchers.SelectMany(voucher => voucher.VoucherDetails).Where(voucher => voucher.VoucherId == hdnVoucherId.Value.ToGuid())).Sum(totalDebit => totalDebit.DebitAmount).ToString()); // Debit Amount;
            ((Label)e.Item.FindControl("lblCredit")).Text = ((vouchers.SelectMany(voucher => voucher.VoucherDetails).Where(voucher => voucher.VoucherId == hdnVoucherId.Value.ToGuid())).Sum(totalCredit => totalCredit.CreditAmount).ToString());//Credit Amount;   
        }
    }

No comments:

Post a Comment