CREATE PAGING IN XSLT USING ASP.NET IN C#
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="xslt._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Xml ID="Xml1" DocumentSource="product.xml" TransformSource="product.xslt" runat="server" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
namespace xslt
{
public partial class _default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data
Source=192.168.1.6;Initial Catalog=Sample_Ecommerce;Integrated
Security=True");
protected void Page_Load(object sender, EventArgs e)
{
string strSQL = "Select *
from Product_Master";
SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
dt.Fill(ds, "product");
//
ds.WriteXml(Server.MapPath("product.xml"));
double CurrentPage = 1D;
if (Request.Params["page"] != null) CurrentPage = Double.Parse(Request.Params["page"]);
XsltArgumentList xal = new XsltArgumentList();
xal.AddParam("CurrentPage", String.Empty,
CurrentPage);
Xml1.TransformArgumentList = xal;
}
#region
Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
//
CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Global.aspx
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
namespace WebApplication1
{
/// <summary>
/// Summary description for Global.
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
}
protected void Session_Start(Object sender, EventArgs e)
{
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
}
protected void Application_Error(Object sender, EventArgs e)
{
}
protected void Session_End(Object sender, EventArgs e)
{
}
protected void Application_End(Object sender, EventArgs e)
{
}
#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
Product.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<product>
<product_id>1</product_id>
<Productname>bata</Productname>
<Productdes>Shoe 123 Desd</Productdes>
<Image>/image/2309210image2.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Createdby>admin</Createdby>
<created>2013-12-05T00:00:00+05:30</created>
<uom>1</uom>
<OpeningStock>2</OpeningStock>
<OpeningDate>2013-11-11T00:00:00+05:30</OpeningDate>
<ClosingStock>14</ClosingStock>
<ClosingDate>2013-11-11T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>10</product_id>
<Productname>Glasses</Productname>
<Productdes>Fast track</Productdes>
<Image>/image/822984029577406797735Car.jpg</Image>
<Price>6765</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>Fast track</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>11</product_id>
<Productname>Glasses</Productname>
<Productdes>Glasses</Productdes>
<Image>/image/83004058288584Jellyfish.jpg</Image>
<Price>456</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>Glasses</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>12</product_id>
<Productname>TShirt</Productname>
<Productdes>Description</Productdes>
<Image>/image/800690Koala.jpg</Image>
<Price>100</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>TShirt</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>13</product_id>
<Productname>TShirt Adidas</Productname>
<Productdes>TShirt Adidas Desc</Productdes>
<Image>/image/5672546Koala.jpg</Image>
<Price>100</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>TShirt Adidas</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>14</product_id>
<Productname>Shoe123</Productname>
<Productdes>aaaaa</Productdes>
<Image>/image/9476495Koala.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>15</product_id>
<Productname>ProductName</Productname>
<Productdes>descr</Productdes>
<Image>/image/377319329577406797735Car.jpg</Image>
<Price>100</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>2</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>16</product_id>
<Productname>test</Productname>
<Productdes>test</Productdes>
<Image>/image/9514139image4.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>test</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>17</product_id>
<Productname>test1</Productname>
<Productdes>test</Productdes>
<Image>/image/3184058image4.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>test</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>18</product_id>
<Productname>test1</Productname>
<Productdes>test</Productdes>
<Image>/image/2143022image4.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>test</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>19</product_id>
<Productname>new</Productname>
<Productdes>new</Productdes>
<Image>/image/9441356image2.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>new</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>2</product_id>
<Productname>dice1</Productname>
<Productdes>Shoe 123 Desd</Productdes>
<Image>image/64733386797735Car.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Createdby>admin</Createdby>
<created>2013-12-05T00:00:00+05:30</created>
<uom>1</uom>
<OpeningStock>6</OpeningStock>
<OpeningDate>2013-11-11T00:00:00+05:30</OpeningDate>
<ClosingStock>160</ClosingStock>
<ClosingDate>2013-11-11T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>20</product_id>
<Productname>nike</Productname>
<Productdes>nike</Productdes>
<Image>/image/86933837319179image4.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-19T00:00:00+05:30</created>
<Brandname>nike</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>21</product_id>
<Productname>nike</Productname>
<Productdes>nike</Productdes>
<Image>/image/26031747319179image4.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-19T00:00:00+05:30</created>
<Brandname>nike</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>3</product_id>
<Productname>adidas</Productname>
<Productdes>adidas Desc</Productdes>
<Image>/image/74887847319179image4.jpg</Image>
<Price>100</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-13T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock>10</OpeningStock>
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock>100</ClosingStock>
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>4</product_id>
<Productname>paragon</Productname>
<Productdes> adidas Description</Productdes>
<Image>/image/90989162309210image2.jpg</Image>
<Price>1000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-13T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock>10</OpeningStock>
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock>50</ClosingStock>
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>5</product_id>
<Productname>slipper</Productname>
<Productdes>adidas</Productdes>
<Image>/image/189346729577406797735Car.jpg</Image>
<Price>70000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-16T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock>10</OpeningStock>
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock>9</ClosingStock>
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>6</product_id>
<Productname>sss</Productname>
<Productdes />
<Image>/image/2194484image3.jpg</Image>
<Price>0</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-16T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock>10</OpeningStock>
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock>50</ClosingStock>
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>7</product_id>
<Productname>dice</Productname>
<Productdes>adidas</Productdes>
<Image>/image/55343757319179image4.jpg</Image>
<Price>10</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-16T00:00:00+05:30</created>
<Brandname>adidas</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>8</product_id>
<Productname>TShirt</Productname>
<Productdes>TShirt Description</Productdes>
<Image>/image/189346729577406797735Car.jpg</Image>
<Price>100</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>BrandName</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
<product>
<product_id>9</product_id>
<Productname>shirt</Productname>
<Productdes>basics</Productdes>
<Image>/image/8288584Jellyfish.jpg</Image>
<Price>7000</Price>
<status>Yes</status>
<Modifiedby />
<Modified>1900-01-01T00:00:00+05:30</Modified>
<Createdby>admin</Createdby>
<created>2013-12-18T00:00:00+05:30</created>
<Brandname>basics</Brandname>
<uom>1</uom>
<OpeningStock />
<OpeningDate>1900-01-01T00:00:00+05:30</OpeningDate>
<ClosingStock />
<ClosingDate>1900-01-01T00:00:00+05:30</ClosingDate>
</product>
</NewDataSet>
product.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:param name="CurrentPage"/>
<xsl:param name="PageSize" select="5" />
<xsl:param name="MaxPages" select="5" />
<xsl:template match="/NewDataSet">
<h1>NewDataSet</h1>
<xsl:call-template name="Pages">
</xsl:call-template>
<table border="1">
<tr>
<th>position</th>
<th>product_id</th>
<th>Productname</th>
<th>
Productdes
</th>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template name="Pages">
<xsl:variable name="TotalItems" select="count(product)" />
<xsl:variable name="Pages" select="ceiling($TotalItems div $PageSize)" />
<p>
Current page:
<xsl:value-of select="$CurrentPage"/>
<br/>
Total products:
<xsl:value-of select="$TotalItems"/>
<br/>
Total pages:
<xsl:value-of select="$Pages"/>
<br/>
</p>
<xsl:for-each select="product[((position()-1)
mod $PageSize = 0)]">
<xsl:choose>
<xsl:when test="(position() > ($CurrentPage - ceiling($MaxPages div 2))
or position() > (last() - $MaxPages)) and ((position() < $CurrentPage +
$MaxPages div 2) or (position() < 1 + $MaxPages))">
<xsl:if test="position()=$CurrentPage">
[ <xsl:value-of select="position()"/> ]
</xsl:if>
<xsl:if test="position()!=$CurrentPage">
[ <xsl:text disable-output-escaping="yes"><a
href="default.aspx?page=</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>
<xsl:value-of select="position()"/>
<xsl:text disable-output-escaping="yes"></a> ] </xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="position()=1">
<xsl:text disable-output-escaping="yes"><a href="default.aspx?page=</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>
First<xsl:text disable-output-escaping="yes"></a></xsl:text>
</xsl:when>
<xsl:when test="position()=last()">
<xsl:text disable-output-escaping="yes"><a href="default.aspx?page=</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>
Last
<xsl:text disable-output-escaping="yes"></a> </xsl:text>
</xsl:when>
<xsl:when test="(position() > ($CurrentPage - ceiling($MaxPages div 2)
- 1) or position() > (last() - $MaxPages) - 1 ) and ((position() < $CurrentPage +
$MaxPages div 2 + 1) or (position() < 2 + $MaxPages))">
<xsl:text disable-output-escaping="yes"> <a href="default.aspx?page=</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>...<xsl:text disable-output-escaping="yes"></a> </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="product">
<xsl:choose>
<xsl:when test="(position() >= 1 + ($CurrentPage - 1) * $PageSize) and
(position() < (1 + $CurrentPage * $PageSize))">
<tr>
<td>
<xsl:value-of select="position()"/>
</td>
<td>
<a>
<xsl:attribute name="href">
WebForm1.aspx?ID=<xsl:value-of select="product_id"/>
</xsl:attribute>
<xsl:value-of select="product_id"/>
</a>
</td>
<td>
<xsl:value-of select="Productname"/>
</td>
<td>
<xsl:value-of select="Productdes"/>
</td>
</tr>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>