Repeater split after X returned results - c#

I have the following code that outputs a number of list items <li> wrapped within a UL
<asp:Repeater ID="RT_Footer" runat="server">
<HeaderTemplate>
<ul class="ftr-links"></HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="HL_NAV" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "PageName") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'></asp:HyperLink></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
After 7 items are repeated id like to then wrap the next 7 within another UL, is this possible?
Im not a .net developer and Ive been left with the website built by an existing developer.
Any help would be greatly appreciated, thanks

Using ListView is better suited for this than Repeater: It offers to repeat a configurable count of items in a group.
<asp:ListView ID="RT_Footer" runat="server" GroupItemCount="7">
<LayoutTemplate>
<ul runat="server" id="groupPlaceholder" />
</LayoutTemplate>
<GroupTemplate>
<ul class="ftr-links">
<li id="itemPlaceholder" runat="server" />
</ul>
</GroupTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="HL_NAV" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "PageName") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' />
</li>
</ItemTemplate>
</asp:ListView>
If you need, you can even throw in a <GroupSeparatorTemplate> to go between your groups.

Related

Listview not displaying 2 items in a row even though GroupItemCount is used

I am still new in asp.net c# and want to display a listview with 2 items in a row. However, the listview is not displayed with 2 items in a row even though I use GroupItemCount. Can anyone tell me how to solve it?
Thank you in advance.
<asp:ListView ID="lvClientFacility" runat="server" DataSourceID="SqlDataSource1" DataKeyName="facility_id" GroupItemCount="2" GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupPlaceholder1" runat="server" />
</div>
</LayoutTemplate>
<GroupTemplate>
<div>
<asp:PlaceHolder ID="itemPlaceholder1" runat="server" />
</div>
</GroupTemplate>
<ItemTemplate>
<a class="facility" href="clientReservation.aspx?Id=<%# Eval("facility_id")%>">
<asp:Image ID="Image1" runat="server" Height="200px" Width="250px" ImageUrl='<%#"data:img/jpg;base64," + Convert.ToBase64String((byte[])Eval("facility_image"))%>' />
<div class="facilityName"><%# Eval("facility_name")%></div>
<asp:LinkButton ID="LinkButton1" PostBackUrl= '<%# "clientReservation.aspx?facility_id=" + Eval("facility_id") %>' runat="server" Text="Book Now"/>
<div class="ClientFacilityDescription"><%# Eval("facility_description")%></div>
</a>
</ItemTemplate>
</asp:ListView>

Postback URL in Asp Repeater

I have a repeater with which I am able to display item. Here the code:
<asp:Repeater
ID="rptHardware"
runat="server">
<ItemTemplate>
<div class="Hardware">
<h1><%#Eval("Name") %></h1>
<br />
<asp:Image runat="server" ImageUrl='<%# "/uploadedimg/" + Eval("ImageName") %>' Width="70%" />
</div>
</ItemTemplate>
</asp:Repeater>
I want to go in a detail page when I click on an item. Where do I put the postback URL? This is what I came up with.
PostBackUrl='<%#Eval("Hardware_ID","bookingdetails.aspx?id={0}")%>'
you can put it on H1 tag of Name section as well like below
<asp:Repeater
ID="rptHardware"
runat="server">
<ItemTemplate>
<div class="Hardware">
<a href='bookingdetails.aspx?id=<%#Eval("Hardware_ID")%>'><h1><%#Eval("Name") %></h1></a>
<br />
<asp:Image runat="server" ImageUrl='<%# "/uploadedimg/" + Eval("ImageName") %>' Width="70%" />
</div>
</ItemTemplate>
</asp:Repeater>

ASP.NET repeater get all items and checkbox if user has right

I have an ASP.NET application and I would like to display all roles and add a checkbox which is checked if the user has the right.
I use an ASP repeater to get all roles but how can I check the checkbox in the same repeater to get the user's role ?
Here is my code:
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked="False" />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked='<%# Convert.ToBoolean(Eval("role")) %>' />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
Make sure you get 'role' field 1 or 0 from DB
Thanks Hardik,
based on your solution, I created a similar method like this: public Dictionary GetAllRolesFromUserToDictionary() so the checkbox will bind to the boolean value
Then the ASP code is like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource3">
<ItemTemplate>
<div>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("Value")%>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetAllRolesFromCurrentUserToDictionary" TypeName="BusinessObject.RoleCollection"></asp:ObjectDataSource>

sql Query to select three of each referenced id

I'm using a SqlDataSource to bring back data to be displayed in a drop down. The headers of each drop down is the brand and in the drop down I want three products of each brand to be brought back.
<asp:SqlDataSource ID="Categories" runat="server" ConnectionString="<%$ ConnectionStrings:Categories %>" SelectCommand="SELECT * FROM CATEGORY WHERE CategoryID <5"></asp:SqlDataSource>
<asp:ListView ID="ListView2" runat="server" DataSourceID="Categories">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:SqlDataSource ID="images" runat="server" ConnectionString="<%$ ConnectionStrings:Categories %>" SelectCommand="SELECT * FROM ITEM ORDER BY Category_ref"></asp:SqlDataSource>
<li id="<%# Eval("CategoryName") %>">
<%# Eval("CategoryName") %>
<div class="dropDown">
<div class="listItem">
<asp:ListView ID="listview3" runat="server" DataSourceID="images">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ItemID","Product.aspx?id={0}") %>'>
<p class="<%# Eval("Category_ref", "brand{0}") %>">
<asp:Image ID="image" CssClass="dropImg" runat="server" ImageUrl='<%# Eval("PhotoPath", "images/products/{0}") %>' />
<br />
<span><%# Eval("ItemName") %></span>
<%# Eval("ItemID").ToString() %>
</p>
</asp:HyperLink>
</ItemTemplate>
</asp:ListView>
</div>
</div>
</li>
</ItemTemplate>
</asp:ListView>
That is my entire navigation with the brand names coming back from database as well as the product information.
The second SqlDataSource query is where I need to fix up so it only selects 3 product. If there is a fourth it will leave it out.
In SQL Server:
SELECT TOP 3 * FROM ITEM ORDER BY Category_ref
In MySQL:
SELECT * FROM ITEM ORDER BY Category_ref LIMIT 3
You should always specify your database of choice with your question, and reduce the problem to the essence (all that ASP code is irrelevant and just scares people from your question).

Data Binding in if statements

In an aspx page i have:
<asp:HyperLink ID="HyperLink" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/storefront.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="lblCustItem" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>
And now I am trying to do:
<%if (Eval("Integration").ToString() == "Y")
{ %>
<asp:HyperLink ID="HyperLink1" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/integration/vendorframe.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="CustItemlbl" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>
<%} %>
<%else
{ %>
<asp:HyperLink ID="HyperLink" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/storefront.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="lblCustItem" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>
<%} %>
the page errors out on the second segment of code. So my question is, am i doing something wrong, and is there a better way to use an if statement, like the conditional if, but i do need to run a new instance of string.format thats why I was thinking that wasn't an option.
Error Message:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
One solution is to use an Inline If:
Text='<%# (Eval("Integration").ToString() == "Y") ? DataBinder.Eval(Container.DataItem, "CustItem") : "" %>'
It's not pretty, but it'll get the job done.
This worked for me. Within the Formview.
<div id="PermDiv" runat="server" visible='<%#(Eval("Permissions").Equals("Edit") ? true : false ) %>'></div>

Categories