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

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135

1 Answers1

1

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.

Ant P
  • 24,820
  • 5
  • 68
  • 105
  • Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'System.Web.UI.WebControls.RepeaterItem'. – rahularyansharma Jan 27 '13 at 16:56
  • Sorry, you may need another `.NamingContainer` there to pick up the `RepeaterItem` of `rptHotels` that `rptRooms` is in. I've updated my answer but can't test this at the minute - does this help? – Ant P Jan 27 '13 at 17:00
  • well its works for me . thanks for this . do you have SDL Tridion experienced in this ? – rahularyansharma Jan 27 '13 at 17:13
  • 1
    I have some experience integrating WebForms and Tridion -- not sure if this is considered off-topic but, if you're interested, we have an Area51 proposal for a Tridion StackExchange that we are trying to get to Beta: http://area51.stackexchange.com/proposals/38335/tridion – Ant P Jan 27 '13 at 17:22
  • i want to get some knowledge about SDL Tridion . is it possible for you to come in private chat on stackoverflow chat room . If you have time ... thanks in advance – rahularyansharma Jan 27 '13 at 17:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23450/discussion-between-rahularyansharma-and-ant-p) – rahularyansharma Jan 27 '13 at 17:26