Question

<asp:Repeater ID="rptHotels" runat="server" OnItemDataBound="rptHotels_ItemDataBound">
                    <ItemTemplate>
                        <div class="hotel-box">
                            <div class="hotel-img">
                                <asp:HiddenField ID="hdnHotelCode" runat="server" Value='<%#Eval("HotelCode")%>' />
                                <a class="preview" href='<%#Eval("ImageURL_Text") %>' title='<%#Eval("HotelName")%>' target="_blank">
                                    <img src='<%#Eval("ImageURL_Text") %>' alt='<%#Eval("HotelName")%>' height="75px"
                                        width="100px" />
                                </a>
                            </div>
                            <div class="hotel_heeading_content">
                                <div class="hotel_heading">
                                    <h2>
                                        <asp:LinkButton ID="lnkHotelDetail" runat="server" OnClick="lnkHotelDetail_Click">                                      
                                            <%#Eval("HotelName")%>
                                            (
                                            <%#Eval("boardType")%>)
                                        </asp:LinkButton>
                                    </h2>
                                </div>
                                <div class="stars">
                                    <span class="stars">
                                        <%#Eval("StarRating")%></span>
                                </div>
                                <div class="hotel_text">
                                    <%#Eval("HotelAddress")%>,
                                    <%#Eval("Destination")%>
                                    ,<%#Eval("Country")%>
                                    <img src="images/ico_point2.png" alt="" id="mapicon" class="mapicon" />
                                    <input type="hidden" id="hdnLatitude" class="hdnLatitude" runat="server" value='<%#Eval("Latitude")%>' />
                                    <input type="hidden" id="hdnLongitude" class="hdnLongitude" runat="server" value='<%#Eval("Longitude")%>' />
                                    <input type="hidden" id="hdnInfoWindow" class="hdnInfoWindow" runat="server" />
                                </div>
                            </div>
                            <p>
                                <asp:Literal ID="ltDes" runat="server"></asp:Literal>
                            </p>
                            <p>
                                <a href="#">more info</a>
                            </p>
                            <div class="btn">

                                <asp:LinkButton ID="lnkPrice" runat="server"  Text=' <%#Eval("totalPrice")%>'  OnClick="lnkHotelDetail_Click" ></asp:LinkButton>


                            </div>
                            <div class="roominfo">
                                <asp:Repeater ID="rptRooms" runat="server">
                                    <HeaderTemplate>
                                        <div class="rooms">
                                            <div class="roominfoheader">
                                                <div class="roomheaderlbl">
                                                    Room Name</div>
                                                <div class="roomheaderlbl">
                                                    Total Room Rate</div>
                                                <div class="roomheaderlbl">
                                                    Book Now</div>
                                            </div>
                                        </div>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <div class="rooms">
                                            <div class="roominforow">
                                                <div class="roominforowlbl">
                                                    <asp:Label ID="lblRoomName" runat="server" Text='<%#Eval("roomCategory") %>'></asp:Label></div>
                                                <div class="roominforowlbl">
                                                    $
                                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("totalRoomRate") %>'></asp:Label></div>
                                                <div class="roominforowlbl">
                                                   <asp:LinkButton ID="lnkBookNow" runat="server"  Text="Book Now" OnClick="lnkBookNow_Click"></asp:LinkButton></div>
                                            </div>
                                        </div>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

I have above HTML for nested repeater .I am able to find the hidden field value which contain hotel Code by following method

protected void lnkHotelDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        var item = (RepeaterItem)btn.NamingContainer;
        HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");



    }

but the problem is now i have to find the hidden field value when a nested repeater item template link button is clicked .You can check that lnkBookNow is link button which is inside the rptRooms repeater.

protected void lnkBookNow_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        var item = (RepeaterItem)btn.NamingContainer;
        HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");


    }

I tried something like this but its not finding hidden field.

Was it helpful?

Solution

The problem here is that lnkBookNow.NamingContainer is rptRooms. This control obviously does not contain hdnHotelCode.

I think you should be able to do this with:

protected void lnkBookNow_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    var item = (RepeaterItem)btn.NamingContainer.NamingContainer.NamingContainer;
    HiddenField hdnHotelCode = item.FindControl("hdnHotelCode") as HiddenField;
}

btn.NamingContainer is a RepeaterItem in rptRooms. The NamingContainer of that is the Repeater itself. Finally, the NamingContainer of rptRooms is the RepeaterItem of rptHotels, in which you want to find your HiddenField.

Note my use of the as keyword instead of an explicit cast - this will protect you from NullReferenceExceptions if FindControl returns null. Of course, you should explicitly check that hdnHotelCode isn't null before you try to access it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top