sql Query to select three of each referenced id - c#

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

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>

asp.net web application and c# code to control the repeaters.

I have been trying to write c# code in .cs file to control the repeaters.
I donot know how to use entity model to get the data from table and execute using repeaters into categories to subcategories...
here is HTML cod :
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<table>
<tr>
<td style="height:50px">
<asp:Label ID="lbl1" runat="server"> <%#Eval("ForumName") %> </asp:Label></td>
<td> By : <asp:Label ID="lbl2" runat="server"> <%#Eval("CeatedBy") %></asp:Label></td>
<td> on <asp:Label ID="lbl3" runat="server"> <%#Eval("DateCreated") %></asp:Label></td> <br /> <br /> </tr>
</table></ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2" >
<ItemTemplate>
<table>
<tr>
<td> <asp:Label ID="lbl4" runat="server" selectcommand="SELECT [ThreadName] FROM [Threads]" > <%#Eval("ThreadName") %></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ForumConnectionString %>" SelectCommand="SELECT [ThreadName] FROM [Threads]" ></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ForumConnectionString %>" SelectCommand="SELECT [ForumName], [CeatedBy], [DateCreated], [IsActive] FROM [Forum1]"></asp:SqlDataSource>
I have to put the thread name below the particular forum...! and so i am not able to figure out the c# code for it by using entity model..!
awaiting for the solution..
thanks ,
Jay
You want to use entity framework to bind your data against those repeaters in codebehind file ?

How to add paging capabilities to the Repeater control in ASP.Net?

I have an asp.net Repeater control .I have to display only 5 rows in the repeater. I searched about it in Google but most of the resources are talking about how to do it with DataSet. In my case, I am using SqlDataSource. So I can have paging in this Repeater control?
ASP.NET Code:
<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource3">
<HeaderTemplate>
<div>
<table border="1">
<thead>
<tr>
<td colspan="3">
<center> <strong>Safety Quizzes Record</strong> </center>
</td>
</tr>
<tr>
<td>
<center> <strong>Quiz Title</strong> </center>
</td>
<td>
<center> <strong>Date & Time</strong> </center>
</td>
<td>
<center> <strong>Score</strong> </center>
</td>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<p>
<%# Eval("Title") %>
</p>
</td>
<td>
<p>
<%# Eval("DateTimeComplete") %>
</p>
</td>
<td>
<p>
<%# Eval("Score") %>
</p>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT dbo.Quiz.Title, dbo.UserQuiz.DateTimeComplete, dbo.UserQuiz.Score
FROM dbo.employee INNER JOIN
dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username INNER JOIN
dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
WHERE (dbo.employee.Username = #Username)">
<SelectParameters>
<asp:Parameter Name="Username" />
</SelectParameters>
</asp:SqlDataSource>
UPDATE:
What I want is showing only 5 items in the Repeater and the others will be listed on the other pages of the Repeater. So how I can have Pagining with SqlDataSource as mentioned above.
The Repeater control does not support paging out-of-the-box.
You could however implement it but it would require you to manually code it.
I would like to recommend you to change the Repeater control for the ListView control
Differences:
Repeater. This control does not support paging, updating, inserting or deleting operations out-of-the-box, if you want these features you would need to manually code them.
ListView. This is the most flexible of all data-bound controls. It's based on templates like the Repeater and DataList controls. It does support CRUD operations automatically mapping the operations agains your data source control. It does support server paging, and you can customize pretty much everything
BTW, since you are using a SqlDataSource, you would have to manually add paging code to your queries or store procedures, you could change that behavior if you use a LinqDataSource or an EntityDataSource
This is a simplified ListView markup:
<asp:ListView runat="server" ID="lv"
DataSourceID="lds" DataKeyNames="emp_id"
EnableModelValidation="false"
EnablePersistedSelection="false"
>
<LayoutTemplate>
<br />
<br />
A static header
<div runat="server" id="itemPlaceHolder"></div>
<br />
<asp:DataPager runat="server" PageSize="4">
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="true"
ShowLastPageButton="true"
/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<div style="background-color:Gray">
First Name
<div><asp:Label runat="server" ID="lbl" Text='<%# Eval("fname") %>'></asp:Label></div>
<br />
<asp:GridView ID="GridView1" runat="server" DataSource='<%# Eval("Achivements") %>' />
<br />
<asp:LinkButton ID="LinkButton1" Text="Select" CommandName="Select" runat="server" />
<asp:LinkButton ID="LinkButton2" Text="Edit" CommandName="Edit" runat="server" />
<asp:LinkButton ID="LinkButton4" Text="Delete" CommandName="Delete" runat="server" />
</div>
</ItemTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
</asp:ListView>
Please use the PagedDataSource shown at link http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.pageddatasource.aspx

Dynamic Id property on items in ListView

I am developing a simple message board in ASP.NET Web forms and I list all posts in a ListView controller. My code looks something like this:
<ItemTemplate>
<article class="post">
<div class="postinfo">
<div class="postauthor">
Author: <strong><%# Eval("Author") %></strong>
</div>
<div class="postdate">
Date: <strong><%# Eval("PostDate", "{0:D}") %></strong>
</div>
<div class="postvotes">
<asp:Button class="postupvote" id='up<%# Eval("Id") %>' runat="server" />
<asp:Label ID="postvotescount_<%# Eval("Id") %>" class="postvotescount" runat="server" Text="<%# Eval("Votes") %> votes"></asp:Label>
<asp:Button class="postdownvote" id='down<%# Eval("Id") %>' runat="server" />
</div>
</div>
<div class="postcontent"><%# Eval("Text") %></div>
</article>
</ItemTemplate>
My problem is the voting functionality. I want to have css id properties, containing the unique database id of the post. That way I will know the Id of the post, it was voted for. So is this possible and if not, how can I achieve this?
Thanks!
You could use ClientIdMode and set it to predictable
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx
<asp:ListView ID="ListView1"
ClientIDMode="Predictable"
ClientIDRowSuffix="ProductID"
DataSourceID="XmlDataSource1" runat="server" >
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
This assumes you are using .Net 4.0 or above.

Repeater split after X returned results

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.

Categories