Coding the Application
You have just completed building the UI of the application. Now it's time to write the code to wire up all the controls. Switch to the Code View of Default2.aspx and import the following namespaces:
Imports System.Net
Imports System.IO
Imports System.Xml
When users select an article category, the RSS document for that category is first downloaded so that you can extract the title of the RSS document. This will be achieved by two helper functions that you will define:
SendRequest() and
GetResponse(). These two functions send a request to a server and then get the response from the server, using HTTP.
Public Function SendRequest( _
ByVal URI As String, _
ByVal requestType As String) As HttpWebRequest
Dim req As HttpWebRequest = Nothing
Try
'---Creates a HTTP request---
req = HttpWebRequest.Create(URI)
req.Method = requestType '---GET or POST---
Catch ex As Exception
Throw New Exception("Error")
End Try
Return req
End Function
Public Function GetResponse( _
ByVal req As HttpWebRequest) As String
Dim body As String = String.Empty
Try
'---Get a response from server---
Dim resp As HttpWebResponse = req.GetResponse()
Dim stream As Stream = resp.GetResponseStream()
'---Use a StreamReader to read the response---
Dim reader As StreamReader = _
New StreamReader(stream, Encoding.UTF8)
body = reader.ReadToEnd()
stream.Close()
Catch ex As Exception
Throw New Exception("Error")
End Try
Return body
End Function
The
LoadRSS() function first loads the XmlDataSource with the RSS document (as specified in the URI parameter). To obtain the feed title, it sends a request to the server using the HttpWebRequest object. It uses the
SendRequest() and
GetResponse() functions to obtain the RSS document. Once the RSS document is obtained, it uses the XPath expression "channel/title" to fetch the title of the feed. The
LoadRSS() function returns the title of the feed.
Public Function LoadRSS( _
ByVal URI As String) As String
Dim req As HttpWebRequest
Dim xmlDoc As XmlDocument = Nothing
Try
'---load the RSS into the XmlDataSource control---
XmlDataSource1.DataFile = URI
'---download the RSS document---
req = SendRequest(URI, "GET")
Dim xmlData As String = GetResponse(req)
xmlDoc = New XmlDocument()
xmlDoc.LoadXml(xmlData)
'---Select the title of the document---
Dim titleNode As XmlNode = _
xmlDoc.DocumentElement.SelectSingleNode("channel/title")
Return titleNode.InnerText
Catch ex As Exception
Return String.Empty
End Try
End Function
The Button_Click event is the event handler for all the 11 buttons. Depending on the button clicked, the RSS document for each article category is downloaded and the Label1 control is set to the title of the feed.
Public Sub Button_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click, _
Button10.Click, Button11.Click
Dim URL As String = String.Empty
Select Case CType(sender, Button).Text
Case "Database" : URL = _
"http://services.devx.com/outgoing/databasefeed.xml"
Case ".NET" : URL = _
"http://services.devx.com/outgoing/dotnet.xml"
Case "C++" : URL = _
"http://services.devx.com/outgoing/cplusfeed.xml"
Case "Recent Tips" : URL = _
"http://services.devx.com/outgoing/recentTipsFeed.xml"
Case "Web Dev" : URL = _
"http://services.devx.com/outgoing/webdevfeed.xml"
Case "Latest" : URL = _
"http://services.devx.com/outgoing/devxfeed.xml"
Case "Enterprise" : URL = _
"http://services.devx.com/outgoing/enterprisefeed.xml"
Case "Wireless / Mobile" : URL = _
"http://services.devx.com/outgoing/wirelessfeed.xml"
Case "XML" : URL = _
"http://services.devx.com/outgoing/xmlfeed.xml"
Case "Java" : URL = _
"http://services.devx.com/outgoing/javafeed.xml"
Case "Open Source" : URL = _
"http://services.devx.com/outgoing/openSourceFeed.xml"
End Select
Label1.Text = LoadRSS(URL)
End Sub
Finally, when the page is loaded for the first time, you will set the default feed to the "Latest Published Articles" category:
Protected Sub Page_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = _
LoadRSS("http://services.devx.com/outgoing/devxfeed.xml")
End Sub
That's it! Press F5 to test the application. You can now click on any category button and the application will display the article summaries that match your choice in the background without a page refresh (see Figure 7). What's more, the animated gif image (Loading…) will be shown when the DataList control is being refreshed. It will go away when the refresh is complete.
In this article, you have seen how to use Atlas to improve the responsiveness of your Web applications. While this article has just touched the surface of what Atlas can do for your Web site, I hope it has inspired you to get startedas well as given you a jumpstart on the learning curve.