 |
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0  | This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

February 16th, 2009, 10:30 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
RssReader.ascx not showing articles
I've managed to create the GetArticlesRss.aspx page and it works. However, the RssReader.ascx user control is not working as expected on the Default.aspx. The control builds successfully but does not show me the lastest articles. I am presently running the app on my local computer. Please advice.
Cheers.
|
|

February 17th, 2009, 05:55 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Can you post the code for your GetArticlesRss page and RssReader control? (both markup and code-behind)...
|
|

March 1st, 2009, 10:47 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Lee,
Thanks for your response and help. The code you wanted me to post follows.
Cheers
GetArticlesRss.aspx
Code:
<%@PageLanguage="C#"AutoEventWireup="true"ContentType="text/xml"MaintainScrollPositionOnPostback="false"
EnableTheming="false"CodeFile="GetArticlesRss.aspx.cs"Inherits="K3.Fit2Play.UI.GetArticlesRss" %>
<headid="Head1"runat="server"visible="false">
</head>
<asp:repeaterid="rptRss"runat="server">
<HeaderTemplate>
<rssversion="2.0">
<channel>
<title><![CDATA[Fit2Play: <%# RssTitle %>]]></title>
<link><%# FullBaseUrl %></link>
<description>Fit2Play: feel better, move better, perform better</description>
<copyright>Copyright 2009 Fit2Play</copyright>
</HeaderTemplate>
<ItemTemplate>
<item>
<title><![CDATA[<%# Eval("Title") %>]]></title>
<author><![CDATA[<%# Eval("AddedBy") %>]]></author>
<description><![CDATA[<%# Eval("Abstract") %>]]></description>
<link><![CDATA[<%# FullBaseUrl + "ShowArticle.aspx?ID=" + Eval("ID") %>]]></link>
<pubDate><%#string.Format("{0:R}", Eval("ReleaseDate")) %></pubDate>
</item>
</ItemTemplate>
<FooterTemplate>
</channel>
</rss></FooterTemplate>
</asp:repeater>
GetArticlesRss.aspx.cs
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using K3.Fit2Play;
using K3.Fit2Play.BLL.Articles;
namespace K3.Fit2Play.UI
{
publicpartialclassGetArticlesRss : BasePage
{
privatestring _rssTitle = "Recent Articles";
publicstring RssTitle
{
get { return _rssTitle; }
set { _rssTitle = value; }
}
protectedvoid Page_Load(object sender, EventArgs e)
{
int categoryID = 0;
// if a CatID param is passed on the querystring, load the category with that ID,
// and use its title for the RSS's title
if (!string.IsNullOrEmpty(this.Request.QueryString["CatID"]))
{
categoryID = int.Parse(this.Request.QueryString["CatID"]);
Category category = Category.GetCategoryByID(categoryID);
_rssTitle = category.Title;
}
List<Article> articles = Article.GetArticles(true, categoryID,
0, Globals.Settings.Articles.RssItems);
rptRss.DataSource = articles;
rptRss.DataBind();
}
}
}
RssReader.ascx
Code:
<%@ControlLanguage="C#"AutoEventWireup="true"CodeFile="RssReader.ascx.cs"Inherits="MB.TheBeerHouse.UI.Controls.RssReader" %>
<divclass="sectiontitle">
<asp:HyperLinkID="lnkRss"runat="server"ToolTip="Get the RSS for this content">
<asp:Imagerunat="server"ID="imgRss"ImageUrl="~/Images/rss.gif"AlternateText="Get RSS feed"/>
</asp:HyperLink>
<asp:Literalrunat="server"ID="lblTitle"/>
</div>
<asp:DataListid="dlstRss"Runat="server"EnableViewState="False">
<ItemTemplate>
<small><%# Eval("PubDate", "{0:d}") %></small>
<br>
<divclass="sectionsubtitle"><asp:HyperLinkRunat="server"ID="lnkTitle"
NavigateUrl='<%# Eval("Link") %>'Text='<%# Eval("Title") %>'/></div>
<%# Eval("Description") %>
</ItemTemplate>
</asp:DataList>
<pstyle="text-align: right;"><small><asp:HyperLinkRunat="server"ID="lnkMore"/></small></p>
RssReader.ascx.cs
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using K3.Fit2Play.UI;
namespace K3.Fit2Play.UI.Controls
{
publicpartialclassRssReader : BaseWebPart
{
public RssReader()
{
this.Title = "RSS Reader";
}
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("Rss Url"),
WebDescription("The Url of the RSS feed")]
publicstring RssUrl
{
get { return lnkRss.NavigateUrl; }
set { lnkRss.NavigateUrl = value; }
}
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("Header Text"),
WebDescription("The header's text")]
publicstring HeaderText
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("Number of columns"),
WebDescription("The grid's number of columns")]
publicint RepeatColumns
{
get { return dlstRss.RepeatColumns; }
set { dlstRss.RepeatColumns = value; }
}
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("More Url"),
WebDescription("The Url of the link pointing to more content")]
publicstring MoreUrl
{
get { return lnkMore.NavigateUrl; }
set { lnkMore.NavigateUrl = value; }
}
[Personalizable(PersonalizationScope.User),
WebBrowsable,
WebDisplayName("More Text"),
WebDescription("The text of the link pointing to more content")]
publicstring MoreText
{
get { return lnkMore.Text; }
set { lnkMore.Text = value; }
}
protectedvoid Page_Load(object sender, EventArgs e)
{
DoBinding();
}
protectedvoid Page_PreRender(object sender, EventArgs e)
{
DoBinding();
}
protectedvoid DoBinding()
{
try
{
if (this.RssUrl.Length == 0)
thrownewApplicationException("The RssUrl cannot be null.");
// create a DataTable and fill it with the RSS data,
// then bind it to the Repeater control
XmlDataDocument feed = newXmlDataDocument();
feed.Load(GetFullUrl(this.RssUrl));
XmlNodeList posts = feed.GetElementsByTagName("item");
DataTable table = newDataTable("Feed");
table.Columns.Add("Title", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Columns.Add("Link", typeof(string));
table.Columns.Add("PubDate", typeof(DateTime));
foreach (XmlNode post in posts)
{
DataRow row = table.NewRow();
row["Title"] = post["title"].InnerText;
row["Description"] = post["description"].InnerText.Trim();
row["Link"] = post["link"].InnerText;
row["PubDate"] = DateTime.Parse(post["pubDate"].InnerText);
table.Rows.Add(row);
}
dlstRss.DataSource = table;
dlstRss.DataBind();
}
catch (Exception)
{
this.Visible = false;
}
}
privatestring GetFullUrl(string url)
{
if (url.StartsWith("/") || url.StartsWith("~/"))
{
url = (this.Page asBasePage).FullBaseUrl + url;
url = url.Replace("~/", "");
}
return url;
}
}
}
|
|

March 2nd, 2009, 01:32 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Most likely culprit is the DoBinding() method in RssReader.ascx.cs.
You will notice that there is a try/catch block here. The problem is that, the way exceptions are handled is to simply set the RssReader control to Visible=false. This means that if any exceptions are encountered, the entire DoBinding() method will fail silently and without warning.
To figure out where the error is happening, you can run the app in debug mode, starting on the home page. Set a breakpoint on the first line in the try block. Then step through and see where the method fails.
There are lots of places where that could be happening. In the part where the rows are being built for the DataTable, any of those values could result in a NullReferenceException. The DateTime.Parse could also fail. It's hard to say, but you can find out if you debug as described above.
|
|

March 2nd, 2009, 03:39 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Lee,
I followed your instructions and everything loads fine until I get to the catch(Exception) block and received the following error message
A first chance exception of type 'System.ApplicationException' occurred in App_Web_7clmc5va.dll
|
|

March 2nd, 2009, 03:53 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
See this part?
Code:
if (this.RssUrl.Length == 0)
thrownewApplicationException("The RssUrl cannot be null.");
That means you have no value for the RssUrl property in the RssReader control. Did you forget to set it in the markup?
|
|
 |