|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhen consuming things like long web services or RSS feeds from external web sources, it might be nice to load all of the important parts on the page first and then load the slower loading part(s) later. This example will demonstrate how to load multiple blocks of RSS headlines after the main body of the page has had a chance to load. While the page is loading, placeholders are displayed to indicate that additional content is coming. After the page loads, the content for the placeholders is retrieved and displayed asynchronously. Oftentimes, there can be a need to load something on a page that may take significantly longer to load than the rest of the page. In some cases, you can use caching to help with this issue, but it may not always be possible or be the only option. If something is going to take longer to load than normal, it is important to display some kind of visual feedback to the user so that they know to wait while something is happening. Retrieving information from a remote server, displaying large sets of data, and photo galleries are just a few examples where delaying the loading of specific content areas would be useful. PrerequisitesThis example uses:
Creating the UserControlYou could do all this on an ASPX page, but for this example, we'll use a The UserControl Source (RSSFeed.ascx)The key areas to look at in the
<%@ Control Language="VB" AutoEventWireup="false"
CodeFile="RSSFeed.ascx.vb" Inherits="RSSFeed" %>
<%@ Register Assembly="RssToolkit" Namespace="RssToolkit" TagPrefix="cc1" %>
<%--
Create a datasource for the RSS feed. For this example, I have used the
RSS Toolkit to consume a RSS feed.
--%>
<cc1:RssDataSource ID="rssData" runat="server"
Url="http://tempuri.com"
MaxItems="5" />
<asp:Panel runat="server" ID="pnlRSS" CssClass="RSSBlock" Width="275">
<asp:Label runat="Server" ID="lblHeader" Text="Headlines" Visible="False" />
<asp:UpdatePanel runat="Server" ID="updateRSS" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView runat="server" ID="mvRSS" ActiveViewIndex="0" >
<%-- This is the default view that will load with the page --%>
<asp:View runat="Server" ID="view1">
<asp:image runat="server" ID="image1"
ImageAlign="middle" ImageUrl="spinner.gif" />
<asp:Label runat="Server" ID="label1"
Text="Loading..." Font-Size="smaller" ForeColor="orange" />
</asp:View>
<%--
This is the where you will display the content that you want to
load AFTER the page loads. In this example, the RSS headlines
will be ret rived and displayed here.
--%>
<asp:View runat="server" ID="view2">
<asp:Repeater runat="Server" ID="rptRSS">
<HeaderTemplate>
<asp:Label runat="Server" id="lblRSSHeader"
Text='<%# lblHeader.Text %>'
Class="RSSHeader" />
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:hyperlink runat="server" ID="linkRSS"
Text='<%# Eval("Title") %>'
NavigateUrl='<%# Eval("Link") %>' />
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</asp:View>
<%-- This is displayed if the content fails to load --%>
<asp:View runat="Server" ID="view3">
<asp:Label runat="Server" ID="lblNoData"
Text="Unable to load headlines at this time."
Font-Size="smaller" ForeColor="orange" />
</asp:View>
</asp:MultiView>
<%--
The timer works in millisecond intervals. A setting of "1"
will begin loading the block almost instantly after the page loads.
--%>
<asp:Timer ID="timerRSS" Interval="1"
OnTick="DisplayRSS" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
The UserControl CodeBehind (RSSFeed.ascx.vb)Note the following key areas:
Option Strict On
Option Explicit On
Imports System.Data
Imports System.Xml
Imports System.Net
Imports System.IO
Partial Class RSSFeed
Inherits System.Web.UI.UserControl
Public WriteOnly Property URL() As String
Set(ByVal value As String)
'Link to the RSS XML page
rssData.Url = value
End Set
End Property
Public WriteOnly Property RSSTitle() As String
Set(ByVal value As String)
'Create a header for the RSS block
lblHeader.Text = value
End Set
End Property
Protected Sub DisplayRSS(ByVal s As Object, ByVal e As EventArgs)
Try
'Download the RSS feed
Dim wc As WebClient
wc = New WebClient()
wc.Proxy.Credentials =
System.Net.CredentialCache.DefaultNetworkCredentials
Dim feed As Byte()
feed = wc.DownloadData(rssData.Url)
'Populate the RSS content block
rptRSS.DataSource = rssData
rptRSS.DataBind()
'Display the loaded content block
mvRSS.ActiveViewIndex = 1
Catch ex As Exception
'Display the "unable to load" content block
mvRSS.ActiveViewIndex = 2
End Try
'Important! Turn the timer off or it will continuously run
'this routine. You could add an increased value to the
'Interval setting if you want it to try again but, at some
'point you need to tell it to stop--unless you are purposefully
'cycling through content.
timerRSS.Enabled = False
End Sub
End Class
Adding the UserControl to a WebpageIn this example, multiple instances of the RSSFeed At the Top of the Page, After the <%@ Page %> Directive<%@ Register Src="RSSFeed.ascx" TagName="RSSFeed" TagPrefix="uc1" %>
In the Page BodyNotice that the control is being passed the values for the RSSTitle and URL properties that we created earlier. <uc1:RSSFeed id="RSSFeed1" runat="server"
RSSTitle="MSDN: United States"
URL="http://msdn.microsoft.com/globalrss/en-us/global-msdn-en-us.xml" />
Running the Example Locally
Points of Interest
Advanced OptionsBy creating a
History
|
||||||||||||||||||||||