刚亲自撰写一篇技术文章,请大家多指教。哈哈。 有的时候我们需要在网页里读取论坛的信息,在传统ASP的时候我们使用的是JS或者是IFRAME,这两种方式都不是很方便,而且对搜索引擎不友好。现在有了.Net,我们有了另一种方式。 要求:论坛需要提供RSS支持。 代码如下: public bool IsSuccess public RssAsyncTask(string rssUrl) // Create delegate. public String GetRssContent() // Define the method that will get called to _dlgt = new AsyncTaskDelegate(DoTheAsyncTask); return result; // Define the method that will get called when // Define the method that will get called if the task //一个自定义的控件,继承自另一个自定义控件。 public string RssUrl int maxRecordNumber = 6; public int MaxRecordNumber RssAsyncTask task; Page.RegisterAsyncTask(asyncTask); static Random r = new Random(); this.Title = doc.SelectSingleNode(“rss/channel/title”).InnerText; writer.WriteBeginTag(“div”); if (doc != null) XmlNodeList items = doc.SelectNodes(“rss/channel/item”); //使用范型进行日期的倒序排列 for (int i = 0; i < maxRecordNumber; i++) #endregion writer.WriteEndTag(“ul”); RenderChildren(writer); base.RenderEnd(writer);
原文首发:http://bbs.5inet.net/topic.aspx?topicid=181
#region task class
//这是一个任务类,执行具体的任务
public class RssAsyncTask
{
private String _rssContent;
private AsyncTaskDelegate _dlgt;
private string rssUrl;
private bool _success;
{
get
{
return _success;
}
}
{
this.rssUrl = rssUrl;
}
protected delegate void AsyncTaskDelegate();
{
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));
}
// start the asynchronous task.
public IAsyncResult OnBegin(object sender, EventArgs e,
AsyncCallback cb, object extraData)
{
//_rssContent = “Beginning async task.”;
IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);
}
// the asynchronous task is ended.
public void OnEnd(IAsyncResult ar)
{
//_rssContent = “Asynchronous task completed.”;
_dlgt.EndInvoke(ar);
}
// 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;
{
get { return rssUrl; }
set { rssUrl = value; }
}
{
get { return maxRecordNumber; }
set { maxRecordNumber = value; }
}
#endregion
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.ExecuteRegisteredAsyncTasks();
}
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.TitleNavigateUrl = doc.SelectSingleNode(“rss/channel/link”).InnerText;
this.ShowTitle = true;
}
base.RenderBegin(writer);
writer.WriteAttribute(“class”, “child2”);
Right(writer);
writer.WriteBeginTag(“ul”);
Right(writer);
{
#region success
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;
}));
{
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”);
}
}
else
{
writer.WriteBeginTag(“pre”);
Right(writer);
writer.Write(task.GetRssContent());
writer.WriteEndTag(“pre”);
}
writer.WriteEndTag(“div”);
}
}
使用方法:
一、注册控件
<%@ Register Assembly=”Controls” Namespace=”Limited.Controls” TagPrefix=”lm” %>
二、调用
<lm:RArticle ID=”RArticle1″ runat=”server” MaxRecordNumber=”10″ RssUrl=”http://bbs.5inet.net/rss.aspx” />
为了简便起见,本程序就没有使用缓存之类的技术了,如有必要,请自行添加。
在asp.net页面中使用异步读取_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在asp.net页面中使用异步读取_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧