<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<html>
  <body>
    <form method=post runat="server">
      <asp:Label runat="server" ID="MoreInfo" />
      <br><br>
      <asp:DataGrid runat="server" ID="Datagrid1"
        DataKeyField="EmployeeID"
        AutoGenerateColumns="False"
        Width="100%"
        OnSelectedIndexChanged="Datagrid1_SelectionChanged"
        AllowPaging="true"
        AllowCustomPaging="true"
        PageSize="3"
        PagerStyle-HorizontalAlign="Right"
        PagerStyle-PageButtonCount="4"
        PagerStyle-Mode="NumericPages"
        PagerStyle-BackColor="Beige"
        PagerStyle-ForeColor="Red"
        OnPageIndexChanged="Datagrid1_PageChanged"
        HeaderStyle-Font-Size="10"
        HeaderStyle-Font-Bold="true"
        HeaderStyle-ForeColor="Red"
        HeaderStyle-BackColor="Yellow"
        HeaderStyle-BorderColor="Red"
        HeaderStyle-BorderWidth="5"
        FooterStyle-BorderColor="Red"
        FooterStyle-BorderWidth="5"    
        ItemStyle-BackColor="LightCyan"
        ItemStyle-ForeColor="DarkBlue"        
        AlternatingItemStyle-BackColor="LightYellow"
        AlternatingItemStyle-ForeColor="Maroon"
        AlternatingItemStyle-Font-Italic="true"
        SelectedItemStyle-ForeColor="Yellow"
        SelectedItemStyle-BackColor="Red"
        SelectedItemStyle-Font-Bold="true">
        <Columns>
          <asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID"/>
          <asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy"/>
          <asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName"/>
          <asp:BoundColumn HeaderText="First Name" DataField="FirstName"/>
          <asp:ButtonColumn CommandName="Select" ItemStyle-Width="20px" Text="<img border=0 src=message.gif>" />
        </Columns>
      </asp:DataGrid>
    </form>
  </body>
</html><script runat="server" language="C#">
  void Page_Load()
  {
    if (!Page.IsPostBack)
    {
      SetGridItemCount();
      BindGrid();
    }
  }
  
  void SetGridItemCount()
  {
    // create the command and the connection
    string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
    string sql = "SELECT COUNT(*) FROM Employees";
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(sql, conn);
    
    conn.Open();
    // execute the command and use the return value for the grid's VirtualItemCount prop
    Datagrid1.VirtualItemCount = (int)cmd.ExecuteScalar();
    conn.Close();
  }
    
  void BindGrid()
  {
    // create the command and the connection
    string connString = "server=(local);database=Northwind;uid=sa;pwd=;";    
    SqlConnection conn = new SqlConnection(connString);
    
    SqlCommand cmd = new SqlCommand("sp_GetEmployeesByPage", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@PageNumber", SqlDbType.Int, 4));
    cmd.Parameters["@PageNumber"].Value = Datagrid1.CurrentPageIndex + 1;
    cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
    cmd.Parameters["@PageSize"].Value = Datagrid1.PageSize;
    
    // open the connection and get the Reader
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
           
    // bind the reader to the DataList
    Datagrid1.DataSource = reader;
    Datagrid1.DataBind();
      
    // close the reader and the connection
    reader.Close();
    conn.Close();
  }
  
  protected void Datagrid1_SelectionChanged(object sender, EventArgs e)
  {
    // create the command and the connection
    string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
    int empID = (int)Datagrid1.DataKeys[Datagrid1.SelectedIndex];
    string sql = "SELECT * FROM Employees WHERE EmployeeID = " + empID.ToString();
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(sql, conn);
      
    // open the connection and get the Reader
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
           
    // show the results
    StringBuilder str = new StringBuilder("");
    reader.Read();
    str.Append("<b>");
    str.Append(reader["FirstName"].ToString());
    str.Append(" ");
    str.Append(reader["LastName"].ToString());
    str.Append("<br>");
    str.Append(reader["Title"].ToString());
    str.Append("<br>");
    str.Append(reader["Address"].ToString());
    str.Append("<br>");
    str.Append(reader["City"].ToString());
    str.Append(", ");
    str.Append(reader["Region"].ToString());
    str.Append("</b><br>");
    str.Append(reader["Notes"].ToString());
    MoreInfo.Text = str.ToString();
      
    // close the reader and the connection
    reader.Close();
    conn.Close();

    // rebind the grid
    BindGrid();
  }
  protected void Datagrid1_PageChanged(Object sender, DataGridPageChangedEventArgs e)
  {
    // deselect the currently selected row, if any
    Datagrid1.SelectedIndex = -1; 
    MoreInfo.Text = "";
    // change the current page and rebind
    Datagrid1.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
  }</script>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetEmployeesByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetEmployeesByPage]
GOSET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GOCREATE PROCEDURE sp_GetEmployeesByPage
@PageNumber int,
@PageSize int
AS-- create a temporary table with the columns we are interested in
CREATE TABLE #TempEmployees
(
ID  int IDENTITY PRIMARY KEY,
EmployeeID int,
LastName nvarchar(20),
FirstName nvarchar(10),
Title nvarchar(30),
TitleOfCourtesy nvarchar(25),
Address nvarchar(60),
City nvarchar(15),
Region nvarchar(15),
Country nvarchar(15),
Notes ntext
)-- fill the temp table with all the employees
INSERT INTO #TempEmployees
(
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
)
SELECT 
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
FROM 
  Employees ORDER BY EmployeeID ASC-- declare two variables to calculate the range of records to extract for the specified page
DECLARE @FromID int
DECLARE @ToID int
-- calculate the first and last ID of the range of records we need
SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
SET @ToID = @PageNumber * @PageSize-- select the page of records
SELECT * FROM #TempEmployees WHERE ID >= @FromID AND ID <= @ToID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO