Paging a GridView with Image buttons

In this blog we will know how to do Paging with Image buttons inside a GridView.

 

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView id="g1" Runat="Server" DataKeyNames="EmpId"

  AllowPaging="True"

  PageSize="2"

  AllowSorting="True"

  AutoGenerateColumns="False"

  CellPadding="2"

  HeaderStyle-BackColor="#E0E0E0"

  PagerSettings-Position="TopAndBottom"

  PagerSettings-Mode="NextPreviousFirstLast"

  PagerSettings-FirstPageImageUrl="~/First.gif"

  PagerSettings-PreviousPageImageUrl="~/Previous.gif"

  PagerSettings-NextPageImageUrl="~/Next.gif"

  PagerSettings-LastPageImageUrl="~/Last.gif"

  PagerStyle-BackColor="#E0E0E0"

  PagerStyle-HorizontalAlign="Center" onpageindexchanging="g1_PageIndexChanging">

  <Columns>

  <asp:BoundField DataField="EmpId" HeaderText="EmpId" ItemStyle-Width="50"/>

  <asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-Width="50"/>

  <asp:BoundField DataField="EmpName" HeaderText="EmpName" ItemStyle-Width="50"/>

  <asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="50"/>

  <asp:BoundField DataField="Phone" HeaderText="Phone" ItemStyle-Width="50"/>

  <asp:BoundField DataField="Salary" HeaderText="Salary" ItemStyle-Width="50"/>

  <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="50"/>

 

  </Columns>

</asp:GridView>

 

    </div>

    </form>

</body>

</html>

 

 

 

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    SqlCommand com ;

    SqlDataAdapter sqlda;

    DataSet ds;

    string str;

  

    protected void Page_Load(object sender, EventArgs e)

    {

        bindgrid();

    }

 

    void bindgrid()

    {

        SqlConnection con = new SqlConnection(connStr);

        con.Open();

        str = "select * from employee";

        com = new SqlCommand(str, con);

        sqlda = new SqlDataAdapter(com);

        ds = new DataSet();

        sqlda.Fill(ds, "employee");

        g1.DataMember = "employee";

        g1.DataSource = ds;

        g1.DataBind();

        con.Close();

    }

    protected void g1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        g1.PageIndex = e.NewPageIndex;

        bindgrid();

    }

}

 

 

Ebook Download
View all
Learn
View all