I'm new with WebForms.
Now I'm using Repeater to display item from List collection.
The problem that I need to change css class depends on evenness of item.
My repeater code:
<asp:Repeater runat="server" ID="rptItems">
<ItemTemplate>
<div class="not-even">
<%# Eval("Title") %>
</div>
<ItemTemplate>
</asp:Repeater>
And what I want:
<div class="not-even">
Item 1
</div>
<div class="even">
Item 2
</div>
<div class="even">
Item 3
</div>
<div class="even">
Item 4
</div>
How can I set CSS class depends on evennes of item using repeater?
You can use a ternary operator for that. Check the ItemIndex and set the correct class.
<asp:Repeater ID="rptItems" runat="server">
<ItemTemplate>
<div class="<%# Container.ItemIndex %2 == 1 ? "even" : "not-even" %>">
Item <%# Container.ItemIndex + 1 %>
</div>
</ItemTemplate>
</asp:Repeater>
The obvious solution is AlternatingItemTemplate which is designed for exactly this.
<asp:Repeater runat="server" ID="rptItems">
<ItemTemplate>
<div class="not-even">
<%# Eval("Title") %>
</div>
<ItemTemplate>
<AlternatingItemTemplate>
<div class="even">
<%# Eval("Title") %>
</div>
<AlternatingItemTemplate>
</asp:Repeater>
Less obvious, but arguably simpler, is to just use CSS with :nth-child(odd) and :nth-child(even).
For example, give the repeater a base css class
<asp:Repeater runat="server" ID="rptItems" class="items">
<ItemTemplate>
<div>
<%# Eval("Title") %>
</div>
<ItemTemplate>
</asp:Repeater>
Would result in
.items :nth-child(odd)
{
color:red;
}
.items :nth-child(even)
{
color:blue;
}
<div class="items">
<div>
Item 1
</div>
<div>
Item 2
</div>
<div>
Item 3
</div>
<div>
Item 4
</div>
</div>
Related
I uses asp.net listview control to display the details. Each item has the group details. For demo purposes group is hard coded.
I want to display the listview as shown below
Right now, I have this
Code:
<asp:ListView ID="HyperLinkListView" runat="server" ViewStateMode="Disabled" ItemPlaceholderID="itemContainer" GroupPlaceholderID="groupContainer">
<LayoutTemplate>
<section class="quick-links">
<div class="row">
<div class="dfwp-column" style="width: 100%">
<div class="slm-layout-main groupmarker">
<ul class="dfwp-list">
<asp:PlaceHolder ID="groupContainer" runat="server" />
</ul>
</div>
</div>
</div>
</section>
</LayoutTemplate>
<GroupTemplate>
<span>Group</span>
<asp:PlaceHolder ID="itemContainer" runat="server" />
</GroupTemplate>
<ItemTemplate>
<li>
<div class="item">
<div class="link-item">
<asp:HyperLink Target="_blank" ID="hyperlink" NavigateUrl='<%# this.LinkToPlay((((SPListItem)Container.DataItem)["VideoFileName"]).ToString()) %>' Text='<%# Eval("Title") %>' runat="server" />
</a>
</div>
</div>
</li>
</ItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
</asp:ListView>
How do I achieve this?
For a flexible solution, you can use nested ListView
You will need to update your HTML and CSS to get the desired appearance.
ASPX Code
<asp:ListView ID="GroupsListView" runat="server" ViewStateMode="Disabled" ItemPlaceholderID="groupContainer" OnItemDataBound="GroupsListView_ItemDataBound">
<LayoutTemplate>
<section class="quick-links">
<div class="row">
<div class="dfwp-column" style="width: 100%">
<div class="slm-layout-main groupmarker">
<asp:PlaceHolder ID="groupContainer" runat="server" />
</div>
</div>
</div>
</section>
</LayoutTemplate>
<ItemTemplate>
<ul class="dfwp-list">
<li><%# Eval("Title") %></li>
<div>
<asp:ListView runat="server" ID="ItemsListView" ItemPlaceholderID="itemContainer">
<LayoutTemplate>
<section class="quick-links">
<div class="row">
<div class="dfwp-column" style="width: 100%">
<div class="slm-layout-main groupmarker">
<ul class="dfwp-list">
<asp:PlaceHolder ID="itemContainer" runat="server" />
</ul>
</div>
</div>
</div>
</section>
</LayoutTemplate>
<ItemTemplate>
<li>
<div class="item">
<div class="link-item">
<asp:HyperLink Target="_blank" ID="hyperlink" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>' runat="server" />
</a>
</div>
</div>
</li>
</ItemTemplate>
</asp:ListView>
</div>
</ul>
</ItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
</asp:ListView>
In the code behind you need to bind the child ListView in parent ItemDataBound event.
protected void GroupsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListView itemsListView = (ListView)e.Item.FindControl("ItemsListView");
if (e.Item.ItemType == ListViewItemType.DataItem)
{
itemsListView.DataSource = ((Group)e.Item.DataItem).Items;
itemsListView.DataBind();
}
}
It is not quite clear what kind of grouping you want. However the ListView is limited in what it can do.
First of all you can only get a grouping by a fixed number of items. You can define that number with the GroupItemCount property. Your code would then look like this
<asp:ListView ID="HyperLinkListView" GroupItemCount="2" runat="server" ViewStateMode="Disabled" ItemPlaceholderID="itemContainer" GroupPlaceholderID="groupContainer">
And generate in html like this
GROUP
My car video
My sample video
GROUP
Another sample video
Item 4
Assuming you want GROUP A and GROUP B etc, you would normally use a binding expression, which looks like this
<GroupTemplate>
<span>Group <%# Container.DataItemIndex %></span>
<asp:PlaceHolder ID="itemContainer" runat="server" />
</GroupTemplate>
However the GroupItemTemplate is not part of the DataBind process, so getting and index based system will not work like that.
So a client side solution is needed if you want to add A, B etc. So first add a placeholder for the alhpa character and give the <span> a class so jQuery can find it. I've used {index} and groupIndex
<GroupTemplate>
<span class="groupIndex">Group {index}</span>
<asp:PlaceHolder ID="itemContainer" runat="server" />
</GroupTemplate>
Now with a simple jQuery method, all the placeholders are replaced with their correct alpha character.
<script type="text/javascript">
$(document).ready(function () {
$('.quick-links .groupIndex').each(function (index, element) {
var char = String.fromCharCode(65 + index);
$(this).html($(this).html().replace('{index}', char));
});
});
</script>
Note that the .quick-links comes from your <section class="quick-links"> part in the LayoutTemplate.
I have textbox and HtmlEditorExtender inside the Repeater Item, the rendering went all wrong! Anybody has any idea how to solve this?
Below is the sample code.
<asp:Repeater ID="rptQuestion" runat="server">
<ItemTemplate>
<div class="row">
<div class="colTitle">
Question <%# Eval("sequence") %>
</div>
<div class="colColon">:</div>
<div class="colContent">
<asp:TextBox ID="tbxQuestion" runat="server" Text='<%# Eval("questionText") %>'
TextMode="MultiLine" Columns="50" Rows="10" CssClass="textbox">
</asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="tbxQuestion_HtmlEditorExtender" runat="server"
TargetControlID="tbxQuestion" DisplaySourceTab="true">
</ajaxToolkit:HtmlEditorExtender>
</div>
</div>
<asp:Repeater ID="rptAnswer" runat="server" DataSource="<%# GetAnswers(Container.DataItem) %>">
<ItemTemplate>
<div class="row">
<div class="colTitle" style="text-align:right">
Answer <%# Eval("sequence") %>
</div>
<div class="colColon">:</div>
<div class="colContent">
<asp:TextBox ID="tbxAnswer" runat="server" Text='<%# Eval("answerText") %>'
TextMode="MultiLine" Columns="50" Rows="10" CssClass="textbox">
</asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="tbxAnswer_HtmlEditorExtender" runat="server"
TargetControlID="tbxAnswer" DisplaySourceTab="true">
</ajaxToolkit:HtmlEditorExtender>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<SeparatorTemplate>
<div class="row"> </div>
</SeparatorTemplate>
</asp:Repeater>
Solved this by added a dummy HTML editor outside of the UpdatePanel to enable correct rendering all the time. The HTML editor is set to absolute and away from the screen.
I have an Asp.net DataList control in my page. It is currently having repeatcolumns set to 4 which will give me 4 columns in each row. But I want to make this responsive and set the value to 1 for smaller screen sizes. Below is my asp.net control:
<asp:DataList runat="server" RepeatDirection="Horizontal" RepeatColumns="4" ID="dd" class="vex-res">
How can I achieve this?
You cannot make DataList to be responsive, because it renders as Table.
Instead, you need to use ListView with bootstrap (or some other responsive framework).
<asp:ListView ID="ListView1" runat="server" ...>
...
<ItemTemplate>
<div class="row">
<div class="col-md-4"><%# Eval("Name") %></div>
<div class="col-md-4"><%# Eval("Email") %></div>
<div class="col-md-4"><%# Eval("Address") %></div>
</div>
</ItemTemplate>
</asp:ListView>
This Work My Home :
<asp:DataList ID="dlCustomers" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" CssClass="row">
<ItemTemplate>
<%-- <div class="row">--%>
<div class="col-sm-4"><!--THUMBNAIL#2-->
<div class="panel-body">
<span class="label label-warning"><%# Eval("status")%></span>
<div class="thumbnail label-success">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/" +Eval("image1").ToString().Trim() %>' Width="150px" Height="150px" />
<div class="caption"><h4>Rp.<small> <%# Eval("harga")%></small></h4>
<strong><%# Eval("judul") %></strong>
<p>
<small>LT:<strong> <%# Eval("luastanah")%> m2</strong> </small> <small> LB : <strong> <%# Eval("luasbangunan")%> m2</strong> </small>
<small>Setifikat : <strong><%# Eval("sertifikasi")%></strong> </small> <br />
<small> Kamar : <strong><%# Eval("kamartidur")%></strong> </small><br />
<small> Kamar Mandi : <strong><%# Eval("kamarmandi")%></strong> </small>
</p>
Lihat Details
</div>
</div>
</div>
</div>
<%--</div>--%>
</ItemTemplate>
</asp:DataList>
Responsive using bootstrap 3.3.6 ..
Coming to this a little late but I found an asp:repeater works well and no additional CSS is required to get the layout repeating horizontally.
<div class="container">
<div class="row">
<asp:Repeater ID="myRepeater" runat="server" DataSourceID="myDataSourceID">
<ItemTemplate>
<div class="col-sm-4">
<%# Eval("itemID")%><br />
<%# Eval("itmName")%>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
I realise that this will result in the total columns in one row adding up to more than 12 but as this will just wrap to the line underneath it shouldn't really matter.
Reference: https://stackoverflow.com/questions/23502342/bootstrap-3-grid-does-it-really-matter-how-many-columns-are-in-a-row
You can also try setting RepeatLayout="Flow". That will cause your datalist to render as a series of <div> elements, which will behave more responsively.
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="uxPosts_ItemDataBound1" cssClass="row" RepeatLayout="Flow" RepeatDirection="Horizontal">
<ItemTemplate>
<div class="col-sm-3 col-md-3 col-lg-3"><%# Eval("Name") %></div>
<div class="col-sm-3 col-md-3 col-lg-3"><%# Eval("Email") %></div>
<div class="col-sm-3 col-md-3 col-lg-3"><%# Eval("Address") %></div>
<div class="col-sm-3 col-md-3 col-lg-3"><%# Eval("Phone") %></div>
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="uxPosts" runat="server" OnItemDataBound="uxPosts_ItemDataBound1" cssClass="row" RepeatLayout="Flow" RepeatDirection="Horizontal">
<ItemTemplate>
<a id="uxLink" runat="server" class="txt_link">
<div class="col-xs-4 col-sm-4 col-md-4 " style="text-align:center;" >
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Descripcion","{0:d}") %>'></asp:Label>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 " style="text-align:center;" >
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Descripcion","{0:d}") %>'></asp:Label>
</div>
<div class="col-xs-4 col-sm-4 col-md-4 " style="text-align:center;" >
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Descripcion","{0:d}") %>'></asp:Label>
</div>
</a>
</ItemTemplate>
</asp:DataList>
You can do it with asp literal, write the code in the string and add it to literal,
suppose you want to show items dynamically
you can do it this way
Add html
<div class="any_css_class">
<div class="any_css_class2">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</div>
and in your cs file
string htmlcode = ""; //code you want in your html
foreach (FetchedDetails fd in yourListOfItems)
{
htmlcode += "<div class='content content-1' onclick=\"dosomething('" + fd.itemId + "')\"> " +
"<div class='fab fa-any_icon'></div> " +
"<h2>" + fd.anyAttribute + "</h2> ";
}
Literal1.Text = htmlcode; //add code to literal
This will do it. It will be responsive but you have to define your css properly :)
<asp:DataList ID="uxPosts" runat="server" OnItemDataBound="uxPosts_ItemDataBound1" cssClass="row" RepeatLayout="Flow" RepeatDirection="Horizontal">
<ItemTemplate>
<a id="uxLink" runat="server" class="txt_link">
<div class=" col-xs-6 col-sm-4 col-md-3 col-lg-3 " style="text-align:center;" >
<div class="pnlborde">
<div class="encabezadofondo">
<asp:Label ID="Label2" runat="server" Height="38px" width="150px" Text='<%# Eval("Descripcion","{0:d}") %>'></asp:Label>
</div>
<br />
<asp:Image ID="uxImage" runat="server" width="65%" Height="90%" align="center" />
<br /><br />
</div>
<br />
</div>
</a>
</ItemTemplate>
</asp:DataList>
I have a 3-level asp.net repeater, and I need to find the 3rd level's items. The code-behind is posted below as well.
Here is my aspx code:
<div class="container">
<asp:Repeater runat="server" ID="rptGrp0" OnItemDataBound="rptGrp0_ItemDataBound">
<HeaderTemplate>
<div id="Grp0" class="rptParent">
</HeaderTemplate>
<ItemTemplate>
<div id="rptParent <%# Eval("Name") %>">
<div class="row-fluid">
<div class="span12">
<h5 class="parentTitle"><%# Eval("Name") %></h5>
</div>
</div>
</div>
<div class="group">
<div id="rptChild <%# Eval("Name") %>">
<div>
<asp:Repeater runat="server" ID="rptGrp1" OnItemDataBound="rptGrp1_ItemDataBound">
<HeaderTemplate>
<div id="Grp1" class="rptChild">
</HeaderTemplate>
<ItemTemplate>
<div class="group">
<div id="rptGrandChildHeader <%# Eval("Name") %>">
<div class="content">
<div class="container">
<div class="row-fluid">
<div class="span12">
<p class="blue">
<asp:Label runat="server" ID="lblChildName" Text='<%# Eval("Name") %>'></asp:Label>
</p>
</div>
</div>
</div>
</div>
</div>
<div id="rptGrandChild <%# Eval("Name") %>">
<asp:Repeater runat="server" ID="rptContent">
<HeaderTemplate>
<div class="c_module">
<div class="container">
<div class="row-fluid">
<div class="span12">
<div class="data_controls">
<div class="clearfix"></div>
</div>
<div class="d_table_module">
<table border="0" class="display" id="tblContent">
<thead>
<tr>
<th>Product</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:100px;">
<asp:Label runat="server" ID="lblProduct" Text='<%# Eval("Product") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
Here is my code-behind.
protected void lnkButton_Click(object sender, EventArgs e)
{
Repeater primary = (Repeater)this.FindControl("rptGrp0");
if (primary != null)
{
// Items.Count = 0.
foreach (RepeaterItem item in primary.Items)
{
Repeater secondary = (Repeater)item.FindControl("rptGrp1");
if (secondary != null)
{
foreach (RepeaterItem b in secondary.Items)
{
Repeater target = (Repeater)b.FindControl("rptContent");
if (target != null)
{
foreach (RepeaterItem c in target.Items)
{
}
}
}
}
}
}
}
The problem is that my repeater.Items.Count is 0.
Please advise, thanks.
Repeater will display only if it is bounded with DataSource. Make sure your repeaters are bounded with a proper DataSource.
Finding the items of the 3rd repeater depends on how you databind it to begin with. If you are binding data in your Page_Load event, then the data is bound after the click event is processed. On your lnkButton_Click event the contents of the repeaters have not yet been bound. The easiest way to access those items is to have an Item_DataBound event to access those items.
Sample code:
<asp:Repeater runat="server" ID="rptContent" OnItemDataBound="rptContent_ItemDataBound">
...
</asp:Repeater>
Code Behind:
protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
e.Item; // This current item
Repeater rpt = (Repeater)(sender);
rpt.Items; // All items
//Do some condition here to test on the item for whatever you want to do
if(e.Item == someCondition)
// some operation here
}
Okay I found the solution. It has to do with the ASP.NET page lifecycle.
After the lnkButton_Click event, a postback occurred, but I needed to "rebind" the repeater datasource on postback, not just initial page_load.
e.g.
protected void Page_Load()
{
if (!IsPostBack)
{
// bind control
}
else
{
// rebind control
}
}
It's my Repeater:
<asp:Repeater ID="RepeaterWeb" runat="server">
<ItemTemplate>
<div class="productWrapper">
<div class="productWrapperImage"><img src="prdimg/<%# Trim(Eval("ProductImage")).ToString()%>" /></div>
<div class="fontTrebuchet"><%# Trim(Eval("ProductShortInfo")).ToString()%></div>
<h3 class="fontUbuntu productBoxName"><%# Trim(Eval("ProductName")).ToString()%></h3>
</div>
<!--This div my problem--><div class="verticalProductsSpacer"></div>
</ItemTemplate>
</asp:Repeater>
I feed the Repeater with 4 rows of data from database, first three <div class="verticalProductsSpacer"></div> are necessary but the last one shouldn't be existed. How can I do it?
User a SeparatorTemplate:
<asp:Repeater ID="RepeaterWeb" runat="server">
<ItemTemplate>
<div class="productWrapper">
<div class="productWrapperImage"><img src="prdimg/<%# Trim(Eval("ProductImage")).ToString()%>" /></div>
<div class="fontTrebuchet"><%# Trim(Eval("ProductShortInfo")).ToString()%></div>
<h3 class="fontUbuntu productBoxName"><%# Trim(Eval("ProductName")).ToString()%></h3>
</ItemTemplate>
<SeparatorTemplate>
<div class="verticalProductsSpacer"></div>
</SeparatorTemplate>
</asp:Repeater>
You can use Jquery to hide it.
$('.verticalProductsSpacer').last().css('display', 'none');