.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Alphabetic Paging using gridview control in Asp.net

Posted By:satyapriyanayak       Posted Date: August 14, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Here iam 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.

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";
    private void CreateAlphaPagings()
        lbTitle.Text = "Alphabetic Paging Sample";
        DataTable dt = new DataTable();

        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);

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

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

        DataList1.DataSource = dt;

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

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

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

        GridView1.DataSource = dtSelect;

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        LinkButton lbkbtnPaging = (LinkButton)e.CommandSource;
        this.ViewState["SelectedText"] = e.CommandArgument.ToString();
    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>
    <form id="form1" runat="server">
    <h2><asp:Label ID="lbTitle" runat="server" Text=""></asp:Label>
        <asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand"
            OnItemDataBound="DataList1_ItemDataBound" RepeatDirection="Horizontal" runat="server">
                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Bind("PageIndex") %>'
                    Text='<%# Bind("PageText") %>'></asp:LinkButton>
        <br />
        <asp:GridView ID="GridView1" runat="server" EmptyDataText="No Records Found">


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend