how to bind a nested Repeater to an objectdatasource - c#

I'm Completely new at ASP.net
My database model is 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?

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.

This Article was Helpful. that was exactly what i needed
https://msdn.microsoft.com/en-us/library/bb510145.aspx#aspnettut32vbnesteddata_topic4

Related

Rendering content in a grid layout

I have a page where i am displaying videos with their title above them. I am currently using a Repeater which renders out a div with the video title and a div with the video iframe html. Everything is coming from a database table where i store the video title and video iframe html.
<asp:Repeater ID="RptVideos" runat="server" OnItemDataBound="RptVideos_ItemDataBound">
<ItemTemplate>
<div class="TrainingVideosVideoContainer">
<div class="Title">
<asp:Label ID="LblVideoTitle" runat="server" />
</div>
<div class="Video">
<asp:Literal ID="LitYoutubeVideo" runat="server" />
</div>
</div>
</ItemTemplate>
My problem being this renders them out one below the other in a list format. I wish to render my videos out in almost a grid format, so two side by side etc. How is best to go about this? HTML Rendering? Repeater? GridVIew?
I found that #Crowcoder was correct in this suggestion to use a DataList control. With this i was able to specify the layout being in a table format with the RepeatColumns being set to 2
<asp:DataList ID="DltVideos" runat="server" OnItemDataBound="DltVideos_ItemDataBound"
RepeatColumns="2" RepeatLayout="Table" Width="100%">
<ItemTemplate>
<div class="TrainingVideosVideoContainer">
<div class="Title">
<asp:Label ID="LblVideoTitle" runat="server" />
</div>
<div class="Video">
<asp:Literal ID="LitYoutubeVideo" runat="server" />
</div>
</div>
</ItemTemplate>
</asp:DataList>

Repeater not firing proper with Divs on page in asp.net

I want to make a slideshow with divs in repeater items. there are two divs with different classes and cannot show page properly with it. Could you help me please to fix the problem?
Design cs is like this:
<div id="wowslider-container1">
<asp:Repeater ID="rpSlideshow" runat="server" >
<ItemTemplate>
<div class="ws_images">
<ul>
<li id="<%# Container.ItemIndex + 1 %>">
<img src="Yazar/data1/images/<%#Eval("Resmi") %>" alt="<%#Eval("AdSoyad") %>" title="<%#Eval("AdSoyad") %>"
id="<%#Eval("SlideID") %>" />
</li>
</ul>
</div>
<div class="ws_bullets">
<div>
<a href="#" title="<%#Eval("SlideID") %>"><span>
<img src="Yazar/data1/tooltips/<%#Eval("Resmi") %>" alt="<%#Eval("AdSoyad") %>" /><%#Eval("SlideID") %></span></a>
</div>
</div>
<div class="ws_shadow">
</div>
</ItemTemplate>
</asp:Repeater>
</div>
PS: I tried to combine divisions like below but it did not work proper.
((HtmlGenericControl)(e.Item.FindControl("myDiv"))).
Attributes["class"] = "id" + indexi++;
You need runat="server" attribute, if you want to access it at code behind.
<div id="myDiv" runat="server">...</div>
TIP: For that purpose, we normally use Panel server control which basically renders html div tag. The advantage is you can use strongly type CssClass property.
<asp:Panel id="MyPanel" runat="server">...</asp:Panel>
Usage: ((Panel)e.Item.FindControl("MyPanel")).CssClass = "id" + indexi++;
Please note that I rename the control id to MyPanel.

Assign a Parent item with Child data in nested repeater C#

Can you set a parent tag (Label) with a child data within a nested repeater?
<asp:Repeater ID="rpDB" runat="server" OnItemDataBound="rpDB_ItemDataBound" EnableViewState="true">
<ItemTemplate>
<label class="glyphicon glyphicon-leaf" /><label id="rbt" runat="server"></label>
<asp:Repeater ID="rpDB_item" runat="server" OnItemDataBound="rpDB_item_ItemDataBound">
<ItemTemplate>
<div class="row anj">
<div class="col-md-1 col-xs-1 glyphicon glyphicon-off" id="lblboot" runat="server" title=<%# DataBinder.Eval(Container.DataItem,"boot") %>></div>
</div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I would like to set label rbt in the parent repeater rpDB with the value of the lblboot in the child repeater rpDB_Item. Is this possible, and if so how. I see plenty pulling parent into child but not reversed.
Found the answer. I thought I had used this but must have been doing it wrong.
(HtmlGenericControl) mycontrol = (HtmlGenericControl)(e.Item.Parent.Parent.FindControl("myControl"));
This allowed me to set the attributes to a parent control.

ASP.Net Repeater Responsive Controls Binding

I have ASP.Net controls for search attributes with check box options and ASP.NET repeater. For Responsive Design, I have same bindings in two different places to match the design.
Here, CheckBoxList ID=Options1 & ID=Options2 both having same binding but the code will repeat. I have 400 lines to check the conditions for single binding. i have to duplicate all the codes again for the another view. Is there any way to optimize the code to single bind with both views handling. ( Avoid duplicate binding and checking)?
// Desktop View
<div class="hidden-sm hidden-xs narrowSearch">
//Content
<asp:Repeater ID="rptAttributes1" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options1" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
// Mobile view
<div class="sec_left hidden-lg hidden-md">
<div class="moremenu narrowSearch">
//Content
<asp:Repeater ID="rptAttribute2" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options2" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
Code Behind:
CheckBoxList chklOptions1 = item.FindControl("Options1") as CheckBoxList;
CheckBoxList chklOptions2 = item.FindControl("Options2") as CheckBoxList;
You can place your repeater (including the declarative code as well as what you have in the code-behind of your page) into a custom user control. Then you put the user control in each section (desktop and mobile) of your page. This is the simplest way to avoid repeated code. Having said this, since you've only provided part of the code that's in the code-behind class of your page, I can't speak to what changes might be required when you move the imperative code you have in your page's code-behind class to the user control.
Good luck!

Adding 'Show More' functionality to a ASP Repeater

To put it as simple as I can. I have a website and the homepage is populated with an ASP Repeater control. This shows details of people you follows activities on the site within the last 30 days.
My problem being I dont want my homepage to be too long on loading if you follow lots of people and they have been participating in lots of recent activities, meaning the repeater will be extremely long and hence my page being stretched.
What I want is for the repeater to show say 15 items/rows then below this is a 'Show more' link which will show either show the rest or each time you click shows another 15 (either/or I will be happy with!)
Here is my repeater code...
<asp:Repeater ID="RptNewsFeed" runat="server"
onitemdatabound="RptNewsFeed_ItemDataBound">
<ItemTemplate>
<div class="NewsFeedItem">
<div class="ProfileImageContainer">
<asp:HyperLink ID="HlkProfileImageLink" runat="server" EnableViewState="false">
<asp:Image ID="ImgProfileImage" runat="server" EnableViewState="false" />
</asp:HyperLink>
</div>
<div class="ProfileLinkContainer">
<asp:HyperLink ID="HlkProfileLink" runat="server" EnableViewState="false" CssClass="ActionDoneBy"></asp:HyperLink>
<asp:Label ID="LblAction" runat="server" EnableViewState="false" CssClass="Action"></asp:Label>
<asp:HyperLink ID="HlkActionObjectLink" runat="server" EnableViewState="false" CssClass="ActionDone"></asp:HyperLink>
</div>
<div class="TimeContainer">
<asp:Label ID="LblActionTime" runat="server" EnableViewState="false"></asp:Label>
</div>
</div>
</ItemTemplate>
Thanks in advance for any help provided!

Categories