RSS Import Into .net - asp.net

I'd like to import an RSS feed from WordPress into a .net site, limiting the import to the most current post, not the whole feed. As of now, I have to hide all posts, except the first, via the :first-child pseudo element in my non-public project's stylesheet, which won't work well in the public site (hidden content).
The following is what I have now:
<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" CssClass="blog-rss">
<ItemTemplate>
<h4><%# XPath("title") %></h4>
<span class="author"><%# XPath("pubDate") %></span><br />
<%# XPath("description") %>
<hr />
</ItemTemplate>
</asp:DataList>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
DataFile="http://blog.domain.com/category/signs/feed/"
XPath="rss/channel/item">
</asp:XmlDataSource>
Any suggestions or modifications to the above?

Try modifying the XPath to only get the first item in the feed
<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
DataFile="http://blog.domain.com/category/signs/feed/"
XPath="rss/channel/item[1]">
</asp:XmlDataSource>

Related

Sitecore Images in Media Library in a Folder are not shown on front end

Its the first time I am working with Media Items.
I have some images in Media Items,
/sitecore/media library/Files/News/Images/
In the content editor I have seen there are 3 images. I just want to display the images which are in the above folder (which are child of above item)
I am using following code:
Asp.Net:
<asp:Label runat="server" ID="lblTest"></asp:Label>
<br/>
<asp:Repeater runat="server" ID="repImages">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# LinkManager.GetItemUrl(Container.DataItem as Item) %>' runat="server" Height="100" Width="100" />
<asp:Label runat="server" Text="<%# GeneralHelper.GetItemField(Container.DataItem as Item) %>"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Code Behind:
Item mediaItem = Sitecore.Context.Database.GetItem("/sitecore/media library/Files/News/Images");
lblTest.Text = "Total Images: "+mediaItem.Children.Count.ToString();
repImages.DataSource = mediaItem.Children;
repImages.DataBind();
OutPut:
Since this is a media item you are trying to resolve the URL for, I believe you need to use the MediaManager instead of the LinkManager.
Sitecore.Resources.Media.MediaManager.GetMediaUrl(item);
More details at: https://briancaos.wordpress.com/2012/08/24/sitecore-links-with-linkmanager-and-mediamanager/

How to make this data control reusable for different content and data?

I'm using ASP.NET Web Forms, and I've got a Repeater control to display some data. Currently it looks like this:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<strong>Details:</strong> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<strong>Status:</strong> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
This list has hover behaviour, etc. and I'd like to reuse the same style of list across my project, just with different data (really just replacing the three lines with <strong> tags in). However, I obviously don't want to copypaste and change it every time it's used, so I thought I'd try and make it into a User Control:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<%= DataString %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And in my corresponding class in CodeBehind:
public string DataString { get; set; }
So my main aim was to do this:
<uc:MyUserControl runat="server" ID="blah" DataString="<strong>Date/Time:</strong><%# DataBinder.Eval(Container.DataItem, "date") %> ETC..." />
But it seemed that ASP.NET wanted to evaluate the <% %> in the string there and then, and I tried different ways to get around it such as this:
<% string theDataString = "<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %>"; %>
<uc:MyUserControl runat="server" ID="blah" DataString='<%= theDataString %>' />
But I ran into trouble again with the closing %> in theDataString, and even after replacing it with something like {0: %} (to escape the % sign: can't remember the exact syntax), which did escape it correctly, I discovered that you can't actually use the <%= %> inside an attribute, which was annoying.
My last-ditch attempt was to set DataString in my page's CodeBehind, and the HTML <strong> rendered correctly, but ASP.NET didn't evaluate the <%# %> and just spat it out literally. So I'm kind of stuck as I can't think of anything else... (by the way I'm still relatively new to ASP.NET)
I'm not even sure this is a good way to go about it - previously I thought about making it into a templated user control but this method seemed simpler and it was something I already knew how to do, or so I thought. So any help with this would be greatly appreciated, thanks!
EDIT: Just to be clear, my emphasis here is on the re-usability of this particular control - so I will be able to display different types of data records in this style of list (clickable, highlight behaviour etc).
If your goal is to have three lines appear as bold instead of normal text, then your best bet is to use something more geared towards website-display than something programmatic and heavy-handed like a dynamic server-side user control. For example:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<span class="displayMode">Date/Time:</span> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<span class="displayMode">Details:</span> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<span class="displayMode">Status:</span> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And then, programmtically show:
<style type="text/css">
.displayMode { font-weight: bold; }
/* and/or */
.displayMode { text-decoration: underline; }
</style>
Use the right tool for the job, in this case, I don't feel as though server-side code is the right way to accomplish this task. If, on the other hand, you need to change the content of the insides of your <li> tags, then yes you would have to do it in C#. However, if that were the case you could easily bind it to a different object or use a different control or inherit the control or any number of other methods. For your specific case, just use standard HTML manipulation techniques.

asp.net gridview edit template http link

I have a label within an Edit Template for a gridview which goes like this:
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Project_ID") %>'></asp:Label>
I would like to turn that label text into a http link like so:
<asp:Label ID="lblProjectID" runat="server" Text='<a href=http://intranet/?<%# Bind("Project_ID") %>> <%# Bind("Project_ID") %></a>'></asp:Label>
So the link would look something like http://intranet/?Project_ID
But that syntax is incorrect. What is the correct way to write that?
This is what you can do on your Label tag.
<asp:TemplateField>
<ItemTemplate>
<a href='<%# String.Format("http://intranet/?Project_ID={0}", Eval("Project_ID")) %>'><%# Eval("Project_ID")%></a>
</ItemTemplate>
</asp:TemplateField>
If you want it to be a link... then just use a link, not a label:
<a href='http://intranet/?<%# Eval("Project_ID") %>'><%# Eval("Project_ID")%></a>
or same thing with HyperLinkField (if you want to use it as a column and not inside EditItemTemplate:
<asp:HyperLinkField DataTextField="Project_ID" DataNavigateUrlFields="Project_ID" DataNavigateUrlFormatString="http://intranet/?{0}" />

ASP.NET Datalist calculate field

I recently decided to implement tax on an ecommerce website I've built for fun more than anything and I've hit a stumbling block.
My implementation works well, tax is applied correctly etc, however a buddy pointed out to me today that prices on product pages are normally displayed inc tax.
I was thinking, rather than editing the Business and Data tier, I could change this on the DataList itself but I can't for the life of me work out how I'd actually do that. I've had a look in some text books and had a search around online but because I don't actually know what I'm looking for, I'm stuck :(.
Datalist:
<asp:DataList ID="list" runat="server" RepeatColumns="2" CssClass="ProductList" RepeatDirection="Horizontal"
Width="542px" EnableViewState="False" onitemcommand="list_ItemCommand">
<ItemTemplate>
<div style="width:271px;">
<h3 class="ProductName"><%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %></h3>
<img width="100" border="0" src="<%# Link.ToProductImage(Eval("Thumbnail").ToString()) %>" alt='<%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %>' />
<%# HttpUtility.HtmlEncode(Eval("Description").ToString()) %>
<p>
<b>Price:</b>
<%# Eval("Price", "{0:c}") %>
</p>
<p>
<asp:Button ID="addToCartButton" runat="server" Text="Add to Basket" CommandArgument='<%# Eval("ProductID") %>' CssClass="SmallButtonText" />
</p>
</div>
</ItemTemplate>
</asp:DataList>
Code behind:
// Retrieve the list of products in a Sub Category
list.DataSource = CatalogAccess.GetProductsInSubCategory(subCategoryId, page, out howManyPages);
list.DataBind();
E.g. if the price in the DB is £5, I'd need it to be displayed in the datalist above as £6 which includes the current UK VAT rate of 20%.
So:
DBPrice * 1.2 = IncVatPrice
I hope this makes sense!
Thanks in advance,
Matt
Instead of
<%# Eval("Price", "{0:c}") %>
use an expression somewhat similar to
<%# String.Format("{0:c}", (Decimal)Eval("Price")*1.2) %>
Or implement a function in codebehind:
protected string IncludeVat(object dataItem)
{
Decimal price = (Decimal)DataBinder.Eval(dataItem, "Price");
return String.Format("{0:c}", price * 1.2);
}
and call it in your DataList like that
<%# IncludeVat(Container.DataItem) %>
http://www.pluralsight-training.net/community/blogs/fritz/archive/2005/12/16/17507.aspx

asp:DataList control with asp:LinkButton inside - something's weird

I'm working through examples in a book trying to learn ASP.NET, and I've stumbled on something strange in there. First of all, if I type it as it's written in the book, VS gives me errors. This is the code as it's written in the book:
<asp:DataList ID="employeesList" runat="server">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text=<%#"View more details about " + Eval("Name")%>
CommandName="MoreDetailsPlease"
CommandArgument=<%#Eval("EmployeeID")%> />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
So, I've plucked at it for a while, and came up with this solution which actually compiles:
<asp:DataList ID="employeesList" runat="server" onitemcommand="employeesList_ItemCommand">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text='View more details about <%# Eval("Name") %>'
CommandName="MoreDetailsPlease" CommandArgument='<%Eval("EmployeeID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
Notice that I've also added the OnItemCommand in the asp:DataList tag, so now I'm able to fire the event as expected.
However, results in the browser isn't what I expect; the Name and Username listed in strong text show just fine, but the Literal control that should show extra details (the EmployeeID field) and the Name field inside the LinkButton won't show their values in the page:
not showing as expected http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg
It should say "EmployeeID: 1" and "View more details about Zak Ruvalcaba"
I guess it's the Eval function that's not working when inside another control, can anyone help me out?
Change the LinkButton as :
<asp:LinkButton ID="detailsButton" runat="server"
Text='<%# Eval("Name", "View more details about {0}") %>'
CommandName="MoreDetailsPlease"
CommandArgument='<%# Eval("EmployeeID") %>' />
Sorry I confused the order of parameters. I updated my answer. Format must be the second parameter.
You can view another question I posted yesterday concerning something eerily similar here:
Need help with Eval inside DataList
I do believe Canavar actually gave the correct answer, however.

Resources