Retrieving FindControl from parent repeater in event of child repeater

3

I'm having trouble getting a field from my parent repeater in the click of my linkbutton that is inside another repeater (child).

My parent repeater contains the report code and other information and my child repeater contains the dates for this report. When the user clicks on a certain date I need to get this code from the parent and also pick the date to do so. But I can not do either one or the other.

Follow my html:

<asp:Repeater ID="Repeater1" OnItemDataBound="rptRelatorioFavoritos_ItemDataBound" runat="server">
<HeaderTemplate>
    <table>
        <tr class="titulo">
            <th colspan="4">Favoritos</th>
        </tr>
        <tr>
            <th width="5%" class="td_titulo">Relatório</th>
            <th width="10%" class="td_titulo">Assunto</th>
            <th width="20%" class="td_titulo">Descrição</th>
            <th width="25%" class="td_titulo">Clique na Data Desejada</th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr class="tr_branco">
        <td class="td_branco">
            <asp:ImageButton CommandArgument='<%#DataBinder.Eval(Container.DataItem, "CodRelatorio")%>' ID="imgbFavorito" ImageUrl="~/Content/imgs/BookDel.gif" OnCommand="imgbFavorito_Command" runat="server" ToolTip="Remover do seus favoritos?" />
            &nbsp;&nbsp;<asp:Label ID="lblCodRelatorio" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CodRelatorio")%>' />
        </td>
        <td class="td_branco">
            <asp:Label ID="lblAssunto" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "AssuntoRelatorio")%>'></asp:Label>
        </td>
        <td class="td_branco">
            <asp:Label ID="lblDescricao" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DescricaoRelatorio")%>'></asp:Label>
        </td>
        <td class="td_branco">
            <asp:Label ID="lblLocal" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LojaRelatorio")%>'></asp:Label>

            <asp:Repeater ID="rptRelFilho" OnItemDataBound="rptRelFilho_OnItemDataBound" OnItemCommand="rptRelFilho_OnItemCommand" runat="server">
                <ItemTemplate>
                    <asp:LinkButton ID="lkbDataRelatorio" runat="server"><%#DataBinder.Eval(Container.DataItem, "DataRelatorio")%></asp:LinkButton>&nbsp;
                </ItemTemplate>
            </asp:Repeater>
            <asp:LinkButton ID="lkbDataRelatorio02" runat="server">...</asp:LinkButton>
        </td>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>

My .cs looks like this:

protected void rptRelFilho_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {

        var itensPai = e.Item.Parent.Parent;
        var lblCodRel = (Label)itensPai.FindControl("CodRelatorio");


        var link = (LinkButton)e.Item.FindControl("DataRelatorio");
        string v = link.Text;


    }

Follow the image of the report so you know what I'm trying to do.

    
asked by anonymous 25.02.2016 / 12:52

1 answer

1

So I understood the whole point is to capture the value of a control outside the child repeater but within the parent repeater? Correct?

See the example I built following this idea:

<asp:Repeater ID="rptPai" runat="server" OnItemDataBound="rptPai_ItemDataBound">
        <HeaderTemplate>
            <table>
                <thead>
                    <th>Código</th>
                    <th>Descrição</th>
                    <th>Clique na data desejada</th>
                </thead>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("Codigo") %></td>
                <td><%# Eval("Descricao") %></td>
                <td>
                    <asp:HiddenField id="hdfCodigoRelatorio" runat="server" Value='<%# Eval("Codigo") %>'/>
                    <asp:Repeater ID="rptFilho" runat="server" OnItemCommand="rptFilho_ItemCommand">
                        <ItemTemplate>
                            <asp:LinkButton ID="lkbDataRelatorio" runat="server">
                                <%# this.GetDataItem().ToString() %>
                            </asp:LinkButton>&nbsp;
                        </ItemTemplate>
                    </asp:Repeater>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

Note that right before the child repeater, called the rptName, I put a HiddenField that receives the report code from that row. It is this control that I will access to get the code:

    protected void rptPai_ItemDataBound(object sender, RepeaterItemEventArgs args)
    {
        if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater childRepeater = (Repeater)args.Item.FindControl("rptFilho");
            HiddenField hdfCodigoRelatorioPai = (HiddenField)args.Item.FindControl("hdfCodigoRelatorio");

            childRepeater.DataSource = CarregarDatas(Convert.ToInt32(hdfCodigoRelatorioPai.Value));
            childRepeater.DataBind();
        }
    }

In the ItemDataBound of the parent repeater I take the code and use it to populate the dates of the child repeater. When you click on one of the dates in the rptFrame, the ItemCommand event is called:

    protected void rptFilho_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        var itensPai = e.Item.Parent.Parent;
        HiddenField hdfCodigoRelatorioPai = (HiddenField)itensPai.FindControl("hdfCodigoRelatorio");

    }

See above that catching Father's items is just the way you were doing, and just give a findControl to bring the control you want. I ran a test and the value of the report code is returned without any problems.

This is just a way to do, in the ParentDataBound of the parent repeater you could already pass the report code and play this for the child repeater as well.

I hope I have been clear. Any questions speak there. :)

    
22.04.2016 / 19:28