|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在经过全球个人PC市场占有90%的微软对asp.net不断优化与整合后,asp.net与微软自身平台的动用上更加的高效,加上asp.net在应用上非常容易上手,相信asp.net仍会是最多客户选用的脚本语言,并会在未来几年继续领跑。asp.net|页面|异步有的时分我们必要在网页里读取论坛的信息,在传统ASP的时分我们利用的是JS大概是IFRAME,这两种体例都不是很便利,并且对搜刮引擎不友爱。如今有了.Net,我们有了另外一种体例。
请求:论坛必要供应RSS撑持。
代码以下:
#regiontaskclass
//这是一个义务类,实行详细的义务
publicclassRssAsyncTask
{
privateString_rssContent;
privateAsyncTaskDelegate_dlgt;
privatestringrssUrl;
privatebool_success;
publicboolIsSuccess
{
get
{
return_success;
}
}
publicRssAsyncTask(stringrssUrl)
{
this.rssUrl=rssUrl;
}
//Createdelegate.
protecteddelegatevoidAsyncTaskDelegate();
publicStringGetRssContent()
{
return_rssContent;
}
publicvoidDoTheAsyncTask()
{
//Introduceanartificialdelaytosimulateadelayed
//asynchronoustask.Makethisgreaterthanthe
//AsyncTimeoutproperty.
WebClientwc=newWebClient();
try
{
_rssContent=wc.DownloadString(rssUrl);
_success=true;
}
catch(Exceptione)
{
_rssContent=e.Message;
}
finally
{
wc.Dispose();
}
//Thread.Sleep(TimeSpan.FromSeconds(5.0));
}
//Definethemethodthatwillgetcalledto
//starttheasynchronoustask.
publicIAsyncResultOnBegin(objectsender,EventArgse,
AsyncCallbackcb,objectextraData)
{
//_rssContent="Beginningasynctask.";
_dlgt=newAsyncTaskDelegate(DoTheAsyncTask);
IAsyncResultresult=_dlgt.BeginInvoke(cb,extraData);
returnresult;
}
//Definethemethodthatwillgetcalledwhen
//theasynchronoustaskisended.
publicvoidOnEnd(IAsyncResultar)
{
//_rssContent="Asynchronoustaskcompleted.";
_dlgt.EndInvoke(ar);
}
//Definethemethodthatwillgetcalledifthetask
//isnotcompletedwithintheasynchronoustimeoutinterval.
publicvoidOnTimeout(IAsyncResultar)
{
_rssContent="Ansynchronoustaskfailedtocomplete"+
"becauseitexceededtheAsyncTimeoutparameter.";
}
}
#endregion
//一个自界说的控件,承继自另外一个自界说控件。
publicclassRArticle
:LPanel
{
#regionproperties
stringrssUrl;
publicstringRssUrl
{
get{returnrssUrl;}
set{rssUrl=value;}
}
intmaxRecordNumber=6;
publicintMaxRecordNumber
{
get{returnmaxRecordNumber;}
set{maxRecordNumber=value;}
}
#endregion
RssAsyncTasktask;
protectedoverridevoidOnInit(EventArgse)
{
base.OnInit(e);
task=newRssAsyncTask(this.rssUrl);
PageAsyncTaskasyncTask=newPageAsyncTask(task.OnBegin,task.OnEnd,task.OnTimeout,null);
Page.RegisterAsyncTask(asyncTask);
Page.ExecuteRegisteredAsyncTasks();
}
staticRandomr=newRandom();
protectedoverridevoidRender(System.Web.UI.HtmlTextWriterwriter)
{
stringrssContent=task.GetRssContent();
XmlDocumentdoc=null;
if(task.IsSuccess)
{
doc=newXmlDocument();
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)
{
#regionsuccess
XmlNodeListitems=doc.SelectNodes("rss/channel/item");
List<XmlNode>nodes=newList<XmlNode>();
foreach(XmlNodenodeinitems)
nodes.Add(node);
//利用范型举行日期的倒序分列
nodes.Sort(newComparison<XmlNode>(delegate(XmlNoden1,XmlNoden2)
{
DateTimed1=DateTime.Parse(n1.SelectSingleNode("pubDate").InnerText);
DateTimed2=DateTime.Parse(n2.SelectSingleNode("pubDate").InnerText);
TimeSpants=d2-d1;
return(int)ts.TotalSeconds;
}));
for(inti=0;i<maxRecordNumber;i++)
{
XmlNodenode=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);
}
}
利用办法:
1、注册控件
<%@RegisterAssembly="Controls"Namespace="Limited.Controls"TagPrefix="lm"%>
2、挪用
<lm:RArticleID="RArticle1"runat="server"MaxRecordNumber="10"RssUrl="http://bbs.5inet.net/rss.aspx"/>
为了烦琐起见,本程序就没有利用缓存之类的手艺了,若有需要,请自行增加。
语言是不是不是最重要的? |
|