All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Visual C++中面向对象程序设计方法
ASP.NET,2.0中XSLT的使用_[Asp.Net教程]
ASP.NET,防盗链源码_[Asp.Net教程]
SQL,Server,索引结构及其使用(三)_[SQL,Server教程]
MDF文件在SQL Server中的恢复技术_[SQL Server教程]
简单自定义实现jQuery验证_[AJAX教程]
.Net环境下基于Ajax的MVC方案_.net资料_编程技术
net在cs程序中加入JS(背景不会变白)_[Asp.Net教程]
SQL语句经验技巧_[SQL Server教程]
asp.net全局异常处理_[Asp.Net教程]
Reading number is top 10 pictures
关于海盗的研究
我国房地产真相
色狗系列
So beauty, will let you spray blood10
The hot big eye big breast beauty1
A man's favorite things4
人美胸美腿更美4
The money of more than 100 countries and regions15
On the verge of extinction of the beach1
9.3阅兵全景图4-陸海空现代化兵种方阵梯队
Download software ranking
网页特效实例大全
Tram sex maniac 2 (H) rar bag8
dreamweaver8中文版
传奇私服架设教程
WebService在.NET中的实战应用教学视频 → 第5集
matrix3
少妇苏霞全本
Sora aoi‘s film--Lust fan wall
Tram sex maniac 2 (H) rar bag16
The king of fighters 97(Mobile phone games-apk)
aaa published in(发表于) 2013/12/8 7:52:12 Edit(编辑)
在asp.net页面中使用异步读取_.net资料_编程技术

在asp.net页面中使用异步读取_.net资料_编程技术

在asp.net页面中使用异步读取_.net资料_编程技术-你的首页-uuhomepage.com

有的时候我们需要在网页里读取论坛的信息,在传统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 nodes = new List();
foreach (XmlNode node in items)
nodes.Add(node);


//使用范型进行日期的倒序排列
nodes.Sort(new Comparison(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" %>


二、调用



为了简便起见,本程序就没有使用缓存之类的技术了,如有必要,请自行添加。






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.