Click here to Skip to main content
Licence 
First Posted 27 Feb 2006
Views 28,921
Bookmarked 14 times

Paging with Datagrid in Asp.net 1.1 and XML

By | 27 Feb 2006 | Article
Paging with Datagrid in Asp.net 1.1 and XML

Introduction

Xml is really good has alot of future when use as database.For Example you want to add your guestbook to Xml database and then use datagrid for showing the data.But you want to add pagging to your datagrid.Here I explain pagging in datagrid and Xml file.

Database

The Part will be stored in an XML file on the server, named PartList.xml. The encoding of the Here is the structure of the XML file:

<PRE><?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="part">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="quantity" type="xs:integer" minOccurs="0" msdata:Ordinal="1" />
          <xs:element name="manufacturer" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
          <xs:element name="color" type="xs:string" minOccurs="0" msdata:Ordinal="3" />
          <xs:element name="price" type="xs:float" minOccurs="0" msdata:Ordinal="4" />
        </xs:sequence>
        <xs:attribute name="partid" type="xs:integer" />
      </xs:complexType>
    </xs:element>
    <xs:element name="NewDataSet" msdata:IsDataSet="true">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element ref="part" />
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <part partid="1">
    <quantity>2</quantity>
    <manufacturer>Torux</manufacturer>
    <color>Red</color>
    <price>1</price>
  </part>
  <part partid="2">
    <quantity>0</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>99.99</price>
  </part>
  <part partid="3">
    <quantity>34</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>22.95</price>
  </part>
  <part partid="4">
    <quantity>3</quantity>
    <manufacturer>Torux</manufacturer>
    <color>clear</color>
    <price>45</price>
  </part>
  <part partid="5">
    <quantity>5</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>12</price>
  </part>
  <part partid="6">
    <quantity>87</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>11.95</price>
  </part>
  <part partid="7">
    <quantity>0</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>655.99</price>
  </part>
  <part partid="8">
    <quantity>65</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>299.99</price>
  </part>
  <part partid="9">
    <quantity>5</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>299.99</price>
  </part>
  <part partid="10">
    <quantity>0</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>1.99</price>
  </part>
  <part partid="11">
    <quantity>45</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>855</price>
  </part>
  <part partid="12">
    <quantity>6</quantity>
    <manufacturer>Pear</manufacturer>
    <color>black</color>
    <price>566.98</price>
  </part>
  <part partid="13">
    <quantity>0</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>4</price>
  </part>
  <part partid="14">
    <quantity>44</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>188.94</price>
  </part>
  <part partid="15">
    <quantity>1</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>99</price>
  </part>
  <part partid="16">
    <quantity>9</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>34.43</price>
  </part>
  <part partid="17">
    <quantity>56</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>19.99</price>
  </part>
  <part partid="18">
    <quantity>534</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>34.88</price>
  </part>
  <part partid="19">
    <quantity>0</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>998.99</price>
  </part>
  <part partid="20">
    <quantity>5</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>5</price>
  </part>
</NewDataSet>

Application code:

In visual studio.net we open new web page(webform1.aspx) ,then add this code:

<asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" 
    PageSize="5"></asp:datagrid>

And in code behind add this code:

 

The guestbook will be stored in an XML file on the server, named guestbook.xml. The encoding of the XML file is changed to ISO-8859-1 to be able to handle special characters. Here is the structure of the XML file:

Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        'Fill the Dataset
        Dim ds As New DataSet()
        ds.ReadXml(Server.MapPath(".") & "\..\Xml\PartList.xml")

        'Set the DataGrid's Source and bind it.
        DataGrid1.DataSource = ds
        If Not IsPostBack Then
            DataGrid1.DataBind()
        End If

        'Dispose Items
        ds.Dispose()
    End Sub

    'Implement the EventHandler for PageIndexChanged
    Private Sub ChangePage(ByVal source As Object, ByVal e As _
    System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
    Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        DataGrid1.DataBind()
    End Sub

ok!now you can test your program.I hope you enjoy!!

Conclusion

I would say that you gain to separate data from processes, and in this matter, XML helps a lot. If you would like to change the looks of the datagrod view, you need only to change the asp.net code file.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

hamidreza Talebi

Web Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

I am IT Engineering and I write web application for 3 years.I love writing web programming such as asp.net,php and java Script.I am know work on XML a new technology for web developer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI don't understand Pinmembermangeles6:02 8 Mar '06  
GeneralRe: I don't understand Pinmemberhamidreza Talebi20:28 30 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Feb 2006
Article Copyright 2006 by hamidreza Talebi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid