neda Derakhshesh
neda Derakhshesh

Reputation: 1163

how to bind a nested Repeater to an objectdatasource

I'm Completely new at ASP.net

My database model is here

enter image description here

As it shows in the above picture I have a table it's name is Tours and it has a column named TourId.

I have an object Data Source in my Webform and a repeater the code is below:

<asp:ObjectDataSource ID="ODSTTitle" runat="server" SelectMethod="GetById" TypeName="ATourRep">
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="0" Name="Id" QueryStringField="CID" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>

            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
            </asp:ObjectDataSource>


<div class="container" id="TourDetail">

            <asp:Repeater ID="RptTourDetail" runat="server" DataSourceID="ODSTTitle" ItemType="Tour" EnableViewState="false">

                <ItemTemplate>
                    <li>

                        <div class="row">



                            <div class="col-md-3">
                                  <%-- NestedRepeater --%>

                                <asp:Repeater ItemType="TourDate" ID="RptTourNested" runat="server" EnableViewState="false">
                                    <ItemTemplate>
                                        <h2>
                                                                                                       <%-- What Should I write Here to have a list of each TourDate --%>

                                        </h2>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </div>

                     <%-- This div works correctly --%>
                            <div class="col-md-6">
                                <h4>
                                    <%# Item.TName %>
                                </h4>
                                <p>
                                    <%# Item.TDes %>
                                </p>
                            </div>



                        </div>

                    </li>

                </ItemTemplate>

            </asp:Repeater>

        </div>

As The code Shows there is a nested repeater that it has h2 tag. i need to show a list of each TourDates in h2 tag.

I dont know how to bind ObjectDataSource1 to use TourId of each Tour and then Get StartDates of that tour. and also what I should write in h2 tag of nested repeater to show satrtdates of that tour ((There is more than one startdate for each tour))

and i have this method in my repository also

 public IQueryable<TourDate> GetById(int Id)
{
    return model.TourDates.Where(e => e.TourId == Id);
}

I used Default value of ObjectDataSource and then bind it by eval but it dosent work.

do you have any suggestion or am I wrong in structre or do you think is there any better than nested repeaters there?

Upvotes: 1

Views: 856

Answers (1)

thepanch
thepanch

Reputation: 353

In the parent repeater you need to implement a Repeater.ItemDataBound event as specified here https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound%28v=vs.110%29.aspx, then inside that event you need to get for each item (row) the information you need and send the datasource to the inner Repeater, and then call for its databind function.

Upvotes: 1

Related Questions