Jun
2
Written by:
Steve Gray
6/2/2010 12:25 PM
That was a mouthful…
The task here is to load an ASP DataList control from XML. I threw in the DataTable part because it adds a little complexity, you can’t directly load XML into a DataTable using .ReadXML, you’ll get
DataTable does not support schema inference from Xml
So, the trick is to us the same method on the DataSet, then get the DataTable from the DataSet.
As a bonus, I’ve thrown in the ASP.NET code to show the DataList being populated. You’ll thank me later.
Imports System.Data
Partial Class eCards_eCards
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim oDT As New DataTable
oDT = loadData()
lstCards.DataSource = oDT
lstCards.DataBind()
End If
End Sub
Function loadData() As DataTable
Dim oDT As New DataTable
Dim oDS As New DataSet
oDS.ReadXml(Server.MapPath("/eCards/eCard.xml"))
oDT = oDS.Tables(0)
Return oDT
End Function
End Class
<asp:DataList RepeatColumns="3" ID="lstCards" runat="server" RepeatDirection="Horizontal"
datakeyfield = "image">
<ItemTemplate>
<table cellspacing="20px">
<tr>
<td style="padding: 0px 0px 00px 25px;"><asp:ImageButton ID="imgThumb" runat="server" CommandArgument='<%# Bind("image") %>' ImageUrl='<%# Bind("imageThumb") %>' /> </td>
</tr>
<tr>
<td style="padding: 0px 0px 10px 25px; text-align:center"><asp:LinkButton ID="lnkCard" runat="server" Text='<%# Bind("label") %>'></asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>