Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jun 2

Written by: Steve Gray
6/2/2010 12:25 PM  RssIcon

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>

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us