locked
accessing data value within master repeater from nested repeater. RRS feed

  • Question

  • User-2066630380 posted

    I'm trying to access the RoleID data in this master repeater dataItem.

    // this works.. so I can retrieve and see the data in the item...
    var data = ((RepeaterItem)e.Item.Parent.Parent).DataItem;

    // but then I get stuck here... don't know how to formulate this correctly? I'm assuming I have to use DataRowView to get access to the data object?
    DataRowView row = (data)(e.Item.DataItem)); // data here shows an error.
    string role = row["RoleID"].ToString();

    please advise

    Friday, July 10, 2015 6:03 AM

Answers

  • User-2066630380 posted

    Just update to anyone interested. I ultimately found a workaround possible better way by just using a hiddenfield with eval and accessing that instead of going up and getting the repeater master values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 11, 2015 10:33 AM

All replies

  • User-271186128 posted

    Hi dolittle,

    DataRowView row = (data)(e.Item.DataItem)); // data here shows an error.

    Will you please explain more details about the error message?

    From your code, the type of the 'data' is RepeaterItem type, but the 'row' type is DataRowView. So, the data type is different.

    Based on my experience, if you want to access the value from repeater control, I suggest you could use FindControl method to get the relevant control, then, get its value.

    You could refer to the following code:

                <asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">
                    <HeaderTemplate>
                        <table>
                            <thead>
                                <tr>
                                    <td>Id</td>
                                    <td>Name</td>
                                    <td>Country</td>
                                </tr>
                            </thead>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tbody>
                            <tr>
                                <td>
                                    <%-- <asp:Label ID="lblID" runat="server" Text='<%# Eval("Id") %>'></asp:Label>--%>
                                    <asp:LinkButton ID="lbnID" runat="server" CommandArgument='<%# Eval("Id") %>' Text='<%# Eval("Id") %>'></asp:LinkButton>
                                </td>
                                <td>
                                    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                                  
                                </td>
                            </tr>
                        </tbody>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    Code Behind:

            protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                RepeaterItem item = Repeater1.Items[e.Item.ItemIndex];
                lblResult.Text = ((Label)item.FindControl("lblName")).Text;
            }

    Best Regards,
    Dillion

    Saturday, July 11, 2015 6:53 AM
  • User-2066630380 posted

    Hi Dillon,

    Thanks for your efforts thus far.

    Let me see if I can clarify. I just want to access a value in the data master repeater called RoleId.  It's not an indexitem. Which begs the question does it need to be?
    There is also no control I would reference to. The value is visible when I hover over the repeater 'data' item and expand results. So how would I take it from there? I need to dig into 'data' somehow.

    If I do decide to go your route, then I thought this would work but  Findcontrol won't work like this. I noticed you're using ItemCommand handler, I'm using ItemDataBound handler.  I'm assuming things would then look slightly different.

    Also I realize 'data' is an object and I can't do a findcontrol on the object. But how then can I convert the object or delve into the data of that object?

    var data = ((RepeaterItem)e.Item.Parent.Parent).DataItem;
                    
    lblResult.Text = ((Label)data.FindControl("lblName")).Text;

    Saturday, July 11, 2015 8:40 AM
  • User-2066630380 posted

    Just update to anyone interested. I ultimately found a workaround possible better way by just using a hiddenfield with eval and accessing that instead of going up and getting the repeater master values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 11, 2015 10:33 AM