有的时候我们需要在网页里读取论坛的信息,在传统asp的时候我们使用的是js或者是iframe,这两种方式都不是很方便,而且对搜索引擎不友好。现在有了.net,我们有了另一种方式。
要求:论坛需要提供rss支持。
代码如下:
#region task class
//这是一个任务类,执行具体的任务
public class rssasynctask
{
private string _rsscontent;
private asynctaskdelegate _dlgt;
private string rssurl;
private bool _success;
public bool issuccess
{
get
{
return _success;
}
}
public rssasynctask(string rssurl)
{
this.rssurl = rssurl;
}
// create delegate.
protected delegate void asynctaskdelegate();
public string getrsscontent()
{
return _rsscontent;
}
public void dotheasynctask()
{
// introduce an artificial delay to simulate a delayed
// asynchronous task. make this greater than the
// asynctimeout property.
webclient wc = new webclient();
try
{
_rsscontent = wc.downloadstring(rssurl);
_success = true;
}
catch (exception e)
{
_rsscontent = e.message;
}
finally
{
wc.dispose();
}
//thread.sleep(timespan.fromseconds(5.0));
}
// define the method that will get called to
// start the asynchronous task.
public iasyncresult onbegin(object sender, eventargs e,
asynccallback cb, object extradata)
{
//_rsscontent = "beginning async task.";
_dlgt = new asynctaskdelegate(dotheasynctask);
iasyncresult result = _dlgt.begininvoke(cb, extradata);
return result;
}
// define the method that will get called when
// the asynchronous task is ended.
public void onend(iasyncresult ar)
{
//_rsscontent = "asynchronous task completed.";
_dlgt.endinvoke(ar);
}
// define the method that will get called if the task
// is not completed within the asynchronous timeout interval.
public void ontimeout(iasyncresult ar)
{
_rsscontent = "ansynchronous task failed to complete " +
"because it exceeded the asynctimeout parameter.";
}
}
#endregion
//一个自定义的控件,继承自另一个自定义控件。
public class rarticle
: lpanel
{
#region properties
string rssurl;
public string rssurl
{
get { return rssurl; }
set { rssurl = value; }
}
int maxrecordnumber = 6;
public int maxrecordnumber
{
get { return maxrecordnumber; }
set { maxrecordnumber = value; }
}
#endregion
rssasynctask task;
protected override void oninit(eventargs e)
{
base.oninit(e);
task = new rssasynctask(this.rssurl);
pageasynctask asynctask = new pageasynctask(task.onbegin, task.onend, task.ontimeout, null);
page.registerasynctask(asynctask);
page.executeregisteredasynctasks();
}
static random r = new random();
protected override void render(system.web.ui.htmltextwriter writer)
{
string rsscontent = task.getrsscontent();
xmldocument doc = null;
if (task.issuccess)
{
doc = new xmldocument();
doc.loadxml(rsscontent);
this.title = doc.selectsinglenode("rss/channel/title").innertext;
this.titlenavigateurl = doc.selectsinglenode("rss/channel/link").innertext;
this.showtitle = true;
}
base.renderbegin(writer);
writer.writebegintag("div");
writer.writeattribute("class", "child2");
right(writer);
writer.writebegintag("ul");
right(writer);
if (doc != null)
{
#region success
xmlnodelist items = doc.selectnodes("rss/channel/item");
list<xmlnode> nodes = new list<xmlnode>();
foreach (xmlnode node in items)
nodes.add(node);
//使用范型进行日期的倒序排列
nodes.sort(new comparison<xmlnode>(delegate(xmlnode n1, xmlnode n2)
{
datetime d1 = datetime.parse(n1.selectsinglenode("pubdate").innertext);
datetime d2 = datetime.parse(n2.selectsinglenode("pubdate").innertext);
timespan ts = d2 - d1;
return (int)ts.totalseconds;
}));
for (int i = 0; i < maxrecordnumber; i++)
{
xmlnode node = nodes[i];
writer.writebegintag("li");
right(writer);
writer.writebegintag("a");
writer.writeattribute("target", "_blank");
writer.writeattribute("href", node.selectsinglenode("link").innertext);
right(writer);
writer.write(node.selectsinglenode("title").innertext);
writer.writeendtag("a");
writer.writeendtag("li");
}
#endregion
}
else
{
writer.writebegintag("pre");
right(writer);
writer.write(task.getrsscontent());
writer.writeendtag("pre");
}
writer.writeendtag("ul");
writer.writeendtag("div");
renderchildren(writer);
base.renderend(writer);
}
}
使用方法:
一、注册控件
<%@ register assembly="controls" namespace="limited.controls" tagprefix="lm" %>
二、调用
<lm:rarticle id="rarticle1" runat="server" maxrecordnumber="10" rssurl="http://bbs.5inet.net/rss.aspx" />
为了简便起见,本程序就没有使用缓存之类的技术了,如有必要,请自行添加。
新闻热点
疑难解答
图片精选