SIGN UP MEMBER LOGIN:    
ARTICLE

Alphabetic Paging using GridView Control

Posted by Satyapriya Nayak Articles | ASP.NET Programming August 01, 2011
Here I am going to show Alphabetic Paging using gridview control. Here user can find data according to respective alphabet link and also can find all the data by clicking all link.
Reader Level:
Download Files:
 

Here I am going to show Alphabetic Paging using gridview control. Here user can find data according to respective alphabet link and also can find all the data by clicking all link.

Create a table Customers with column Cus_Code varchar(50), Cus_Name varchar(50), Cus_address varchar(MAX), Cus_sal int.

Program : Default.aspx.cs code

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
{
    private string cnstr = ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["SelectedText"] = "All";
            CreateAlphaPagings();
            BindGrid(this.ViewState["SelectedText"].ToString());
        }
    }
    private void CreateAlphaPagings()
    {
        lbTitle.Text = "Alphabetic Paging Sample";
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");

        if (this.ViewState["Paging"] == null)
        {
            for (int i = 65; i <= (65 + 25); i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = Char.ConvertFromUtf32(i);
                dr[1] = Char.ConvertFromUtf32(i);
                dt.Rows.Add(dr);
            }

            DataRow drNew = dt.NewRow();
            drNew["PageIndex"] = "All";
            drNew["PageText"] = "All";
            dt.Rows.Add(drNew);

            this.ViewState["Paging"] = dt;
        }
        else
            dt = (DataTable)this.ViewState["Paging"];

        DataList1.DataSource = dt;
        DataList1.DataBind();

    }
    private void BindGrid(string StartAlpha)
    {
        SqlConnection con = new SqlConnection(cnstr);
        string sql = "";

        if (StartAlpha == "All")
            sql = "Select * from Customers Order By Cus_Code asc";
        else
            sql = "Select  * from Customers Where Cus_Name Like '"
+ StartAlpha + "%' Order By Cus_Code asc ";

        SqlDataAdapter da = new SqlDataAdapter(sql, cnstr);
        DataTable dtSelect = new DataTable();
        da.Fill(dtSelect);

        GridView1.DataSource = dtSelect;
        GridView1.DataBind();

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton lbkbtnPaging = (LinkButton)e.CommandSource;
        BindGrid(e.CommandArgument.ToString());
        this.ViewState["SelectedText"] = e.CommandArgument.ToString();
        CreateAlphaPagings();
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (this.ViewState["SelectedText"] != null)
            {
                LinkButton lbkbtnPaging = (LinkButton)e.Item.FindControl("lnkbtnPaging");
                if (this.ViewState["SelectedText"].ToString() == lbkbtnPaging.Text)
                    lbkbtnPaging.Enabled = false;
            }
        }
    }
}

Default.aspx code

<%@ 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>
    <h2><asp:Label ID="lbTitle" runat="server" Text=""></asp:Label>
        <asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand"
            OnItemDataBound="DataList1_ItemDataBound" RepeatDirection="Horizontal" runat="server">
       
        <SeparatorTemplate>
              
            </SeparatorTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Bind("PageIndex") %>'
                    Text='<%# Bind("PageText") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:DataList>
        </h2>
        <br />

        <asp:GridView ID="GridView1" runat="server" EmptyDataText="No Records Found">
        </asp:GridView>

    </div>
    </form>
</body>
</
html>

Running the program its output show below

GridView Control
 

Login to add your contents and source code to this article
share this article :
post comment
 

short and simple...keep it up

Posted by Pushkar Rathod Aug 03, 2011

Thanks

Posted by Satyapriya Nayak Aug 02, 2011

Nice Article

Posted by Shalini Juneja Aug 02, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor