ASP Alternating CSS Issue - asp.net

Currently I have 4 spans with alternating CSS tags.
<% var index =0 ; %>
<% foreach (var item in Model.Take(4)) {
var css = (index%2==0)?"even":"odd";
%>
<span class="featured-products <%= css %>">
<%= Html.Photo(PhotoExtensions.PhotoSizeType.HomepageThumb, item.DefaultPhoto)%>
<%= item.ProductName%>
</span>
<%
index ++ ;
} %>
But I need for the last span to contain an extra CSS tag "last". Ive tried a couple of different methods but they all failed. Any help? Thanks.

Have you tried using the :last-child CSS selector? It may not be necessary to add additional markup if you can achieve what you want through CSS.

If you don't want to use CSS.
<% var index = 0; %>
<% var items = Model.Take(4); %>
<% foreach (var item in items) {
var css = (index%2==0)?"":"odd";
if(index == items.Count - 1)
{
css = " last";
}
%>
<span class="featured-products <%= css %>">
<%= Html.Photo(PhotoExtensions.PhotoSizeType.HomepageThumb, item.DefaultPhoto)%>
<%= item.ProductName%>
</span>
<%
index ++ ;
} %>
Also, I would recommend cleaning this code up. You don't need to put <% and %> everywhere.
Update: A bit cleaner way of writing what you're doing. Definately not the cleanest but a good start.
<%
var index = 0;
var items = Model.Take(4);
foreach (var item in items) {
var css = (index%2==0)?"":"odd";
if(index == items.Count - 1)
{
css = " last";
}
%>
<span class="featured-products <%= css %>">
<%= Html.Photo(PhotoExtensions.PhotoSizeType.HomepageThumb, item.DefaultPhoto)%>
<%= item.ProductName%>
</span>
<%
index ++ ;
}
%>

Related

Creating article pagination

Hi I'm using silverstripe 2.4.7 and I'm having difficulty getting the pagination to work. I've created a function in my page.php to get the latest articles like so
function AllNewsPosts($num=1) {
$news = DataObject::get_one("NewsHolder");
return ($news) ? DataObject::get("NewsEntry", "ParentID > 0", "Date DESC", "", $num) : false;
}
Then when i put this function into the control and pagination tags one article shows up however the links to the concurrent articles do not work - essentially the pagination is not working and I'm not sure how to fix it
<% if AllNewsPosts %>
<% control AllNewsPosts %>
<div class="event">
<h2>$MenuTitle |<span class="date"> $Date.Time $Date.Long</span></h2>
<p>$Content.FirstParagraph</p>
See more about this event
</div>
<% end_control %>
<% else %>
<div class="no-entry">'There are no entries'</div>
<% end_if %>
<% if AllNewsPosts.MoreThanOnePage %>
<div id="PageNumbers">
<p>
<% if AllNewsPosts.NotFirstPage %>
<a class="prev" href="$AllNewsPosts.PrevLink" title="View the previous page"><span class="yellow-background">Prev</span></a>
<% end_if %>
<span>
<% control AllNewsPosts.PaginationSummary(0) %>
<% if CurrentBool %>
<span class="current">$PageNum</span>
<% else %>
<% if Link %>
$PageNum
<% else %>
…
<% end_if %>
<% end_if %>
<% end_control %>
</span>
<% if AllNewsPosts.NotLastPage %>
<a class="next" href="$AllNewsPosts.NextLink" title="View the next page"><span class="yellow-background">Next</span></a>
<% end_if %>
</p>
</div>
<% end_if %>
Any help is much appreciated
Note: The following answer is for Silverstripe 2.4. This should not be used for Silverstripe 3.0+ sites. From 3.0 and onwards the PaginatedList object makes pagination much easier.
You are not setting a limit on how many entries to retrieve in your query, or where to start from.
The following tutorial explains how to apply pagination to a set of data objects exactly as you are trying to do:
http://www.ssbits.com/tutorials/2010/paginating-a-filtered-dataobjectset/
Here is an attempt at altering your function to include limit and start as needed for pagination:
PHP
function AllNewsPosts() {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1)
{
$_GET['start'] = 0;
}
$SQL_start = (int)$_GET['start'];
$newsEntries = DataObject::get('NewsEntry', '', 'Date DESC');
$doSet = new DataObjectSet();
foreach ($newsEntries as $newsEntry) {
if ($newsEntry->canView()) {
$doSet->push($newsEntry);
}
}
$doSet->setPageLimits($SQL_start, 10, $doSet->Count());
return $doSet;
}
Note the above will display 10 items per page. You can change this to however you need per page.

Use asp.net tag to set css class

I try to set the CSS class of each row dynamically for a HTML table, I found a blog with info how to do it but it doesn't seem to work for me.
<% foreach (var item in Model){%>
<tr class= "<% Model.GetRowClass(item, "alt", "reg");%>">
Model.GetRowClass returns a string with the style name, when I run it however no style is applied.
You need to use <%= %> instead of <% %>. Try this:
<% foreach (var item in Model){%>
<tr class='<%= Model.GetRowClass(item, "alt", "reg") %>'>
<% } %>

ASP.net mvc how to comment out html code in page

Is there an easy way to comment out a loop which renders some html and has inline html without deleting anything? I am copying and pasting some code from another project to rebuild a new public front end from a working internal backend.
Below is an example of a sitation in which it would be nice...in asp.net MVC 2
<%
List<VehicleBodyTypeListItem> lstBodyTypes = (List<VehicleBodyTypeListItem>)ViewData["ddBodyType"];
foreach (VehicleBodyTypeListItem bodyType in lstBodyTypes)
{
%>
<a href="<%= Url.Action( "Search", new { BodyTypeID=bodyType.BodyTypeID, BodyType= Url.Encode( Html.WebLinkify( bodyType.BodyType))}) + (string)ViewData["httpCriteria"] %>">
<%= Html.Encode( String.Format( "{0} ({1})", bodyType.BodyType, bodyType.Count.ToString())) %> </a>
<br />
<%
}
%>
I have not completed the method that populates this list yet, and have about 5 more like it further down the page.
The keyboard shortcut is, if you select the section you want commented out is: CTRL + K + C will comment out code. CTRL + K + U will uncomment the code.
Comment a block of code by enclosing it in #* and *#
Do you mean adding comments to the code. If so you just need to add //. Like here:
<%
List<VehicleBodyTypeListItem> lstBodyTypes = (List<VehicleBodyTypeListItem>)ViewData["ddBodyType"];
foreach (VehicleBodyTypeListItem bodyType in lstBodyTypes) // Here there's a comment
{
%>
<a href="<%= Url.Action( "Search", new { BodyTypeID=bodyType.BodyTypeID, BodyType= Url.Encode( Html.WebLinkify( bodyType.BodyType))}) + (string)ViewData["httpCriteria"] %>">
<%= Html.Encode( String.Format( "{0} ({1})", bodyType.BodyType, bodyType.Count.ToString())) %> </a>
<br />
<%
}
%>

ASp.NET MVC2 - Rendering an action link and text on same line

I will like to achieve the following html using Html.ActionLink:
<li>John DoePresident</li>
The name "John Doe" and title "President" will be coming from a staff model. This is what I have:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li><%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %> item.Position</li>
<% } %>
Instead of rendering "item.Position" literally, I will like this string extracted from the model.
TIA.
Try this:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li>
<%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %>
<%=item.Position%>
</li>
<% } %>

HTML 'id' attribute for items in dropdown made from html helper

Can we set an id attribute as I would for something like a table column via:
for an html dropdown list element that is created with a helper such as:
<% for (int i = 0; i < Model.Trx.TransactionHolidayCityCollection.Count; i++)
{%>
<%= i > 0 ? "," : "" %>
<%= DropDownData.HolidayDays().ToList().Find(item => item.Value == Model.Trx.TransactionHolidayCityCollection[i].HolidayCityID.Value.ToString()).Text %>
<%} %>
You don't need a helper, necessarily:
<select id="holidayCities" name="holidayCities">
<% foreach (HolidayCity city in Model.Trx.TransactionHolidayCityCollection) { %>
<option
value="<%=city.HolidayCityID.Value %>"
id="holidayCity_<%=city.HolidayCityID.Value %>"
><%=city.Name %></option>
<% } %>
</select>
If you wanted to use the HtmlHelper you can write:
<%=Html.DropDownList("holidayCities", Model.HolidayCitiesSelectList) %>
Model.HolidayCitiesSelectList should be of type IEnumerable<SelectListItem>
This type of customization isn't avaiable for the built in helpers so you might end up creating your own Html.DropDownList helper method.
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.98).aspx

Resources