The common method of binding data to a DataList or DataGrid is binding to SQLDataReader
or SQLDataSet. In this article we will cover retrieving a remote RSS file and
binding it to a datalist.
On my WWWCoder site, I accept RSS file submissions as part of accepting RSS files,
I accept the feed URL from a textbox, retrieve the RSS file, and then bind this
XML to the grid for display. If the gird throws and error, it is due to the XML
file not being properly formatted. The following code demonstrates binding the RSS
file to the datalist.
The first thing we'll do is place a datagrid in the aspx page. The ID of
the list is dataRSSItems and bound to the following nodes from the XML file:
"title", "description", and "link".
<asp:DataList ID="dataRSSItems" Runat="server" RepeatLayout="Table"
AlternatingItemStyle-BackColor=AliceBlue
SelectedItemStyle-BackColor=#ffff00
RepeatDirection="Vertical" RepeatColumns="1">
<SelectedItemStyle BackColor="Yellow"></SelectedItemStyle>
<ItemTemplate>
<asp:LinkButton ID="btnTitle" CssClass="subsubhead"
Text='<%#Container.DataItem("title")%>' Runat="server"
CommandName="select">
</asp:LinkButton>
<br>
<asp:Label Runat="server" ID="lblRssDescription"
CssClass="normal" Text='<%#Container.DataItem("description")%>'>
</asp:Label>
<br>
<asp:HyperLink target="window" ID="lnkSiteURL"
CssClass="normal" Runat="server"
NavigateUrl='<%#Container.DataItem("link")%>'
text='<%#Container.DataItem("link")%>'>
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>
You can see from the code above that we have to refer to our XML nodes by
name within the RSS file. Then in the code behind we'll make a request for a
remote RSS file and bind this XML feed to our datalist. The first function
receives some string containing the value of the RSS file URL to retrive from
teh Internet. You can see the function GetXMLContent will return an XMLReader.
This XMLReader will then be bound to the datalist using the BindRSSFeeds method.
Private Function GetXMLContent(ByVal ContentURL As String) _
As XmlReader
Try
'create an HTTP request.
Dim wr As HttpWebRequest = CType(WebRequest.Create(ContentURL), _
HttpWebRequest)
wr.Timeout = 10000 ' 10 seconds
'get the response object.
Dim resp As WebResponse = wr.GetResponse()
Dim stream As stream = resp.GetResponseStream()
' load XML document
Dim reader As XmlTextReader = New XmlTextReader(stream)
reader.XmlResolver = Nothing
Return reader
Catch ex As Exception
'return some error code.
End Try
End Function
Private Sub BindRSSFeeds(ByVal inURL As String)
Try
Dim myRSSFeed As New DataSet
myRSSFeed.ReadXml(GetXMLContent(inURL))
'now refer to the item node, which we can then obtain
'all our RSS news items underneath the path.
dataRSSItems.DataSource = myRSSFeed.Tables("item")
dataRSSItems.DataBind()
Catch ex As Exception
'return some error code.
End Try
End Sub
Now that our gird is defined and we have the methods written to obtain the
RSS feed and bind it to the grid, we'll place a button on our form to handle an
event from the user, and a textbox control to accept a URL submission in the
form.
<asp:Textbox ID="txtRSSURL" runat="Server" Columns="45">
http://
</asp:TextBox>
<asp:LinkButton ID="cmdSubmitRSS" Runat="server"
CssClass="CommandButton">
Submit RSS File
</asp:LinkButton>
Now in our code behind page we'll capture the link click and then call our
BindRSSFeeds with the value of the textbox passed to the method to get the XML
and bind it to the datalist.
Private Sub cmdSubmitRSS_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles cmdSubmitRSS.Click
Try
BindRSSFeeds(txtRSSURL.Text)
Catch ex As Exception
'throw some error.
End Try
End Sub
Now we should end up with a datalist containing the RSS items from the remote
RSS file.
By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.