ASPX
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Untitled Page</title>
<link type=”text/css” rel=”Stylesheet” href=”GridStyle.css” />
<script type=”text/javascript” src=”App_Scripts/JScript2.js”>
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td>
Country Name:
</td>
<td>
<asp:TextBox ID=”txtCountries” runat=”server”>
</asp:TextBox>
</td>
<td>
<asp:Button ID=”btnpopulate” runat=”server”
Text=”Populate” OnClick=”btnpopulate_Click”/>
</td>
</tr>
<tr>
<td colspan=”3″>
<asp:GridView ID=”gvwCountries” runat=”server”
AllowPaging=”true”
AllowSorting=”true”
AutoGenerateColumns=”false”
OnRowDataBound=”gvwCountries_RowDataBound”
OnSorting=”gvwCountries_Sorting” >
<EmptyDataTemplate>
No Rows to Display
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText=”Serial No”>
<ItemTemplate>
<%# Container.DataItemIndex %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Country Name” SortExpression=”countryName”>
<ItemTemplate>
<asp:Label ID=”lblCountries” runat=”server”
Text = ‘<%# DataBinder.Eval(Container.DataItem, “countryName”) %>’>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID=”Btn_Previous” runat=”server”
CommandName=”Previous”
OnCommand=”ChangePage”
Text=”Previous” >
</asp:LinkButton>
Page
<%–<asp:Label ID=”lblCurrentPage” runat=”server”>
</asp:Label>–%>
<asp:DropDownList ID=”ddlPages” runat=”server” AutoPostBack=”true” OnSelectedIndexChanged=”ddlPages_SelectedIndexChanged”>
</asp:DropDownList>
of
<asp:Label ID=”lblTotalPages” runat=”server”>
</asp:Label>
<asp:LinkButton ID=”Btn_Next” runat=”server”
CommandName=”Next”
OnCommand=”ChangePage”
Text=”Next” >
</asp:LinkButton>
</PagerTemplate>
<AlternatingRowStyle CssClass=”altrowstyle” />
<HeaderStyle CssClass=”headerstyle” />
<RowStyle CssClass=”rowstyle” />
<PagerStyle CssClass=”pagerstyle” />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
ASPX.CS
#region “Local Variables and Declarations”
SqlParameter[] parmCountries = new SqlParameter[10];
protected static int currentPageNumber = 1;
private const int PAGE_SIZE = 10;
int parmCount = 3;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
#endregion
#region “Page Load Events”
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
#endregion
#region “Populate Button Events”
protected void btnpopulate_Click(object sender, EventArgs e)
{
currentPageNumber = 1;
BindGrid();
}
#endregion
#region “GridView Events”
protected void gvwCountries_RowDataBound(object sender, GridViewRowEventArgs e)
{
string mySortExpression = (Session[“mySortExpression”] != null ? Session[“mySortExpression”].ToString() : “”);
GridView gridView = (GridView)sender;
if (mySortExpression.Length > 0)
{
int cellIndex = -1;
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == mySortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
if (e.Row.RowType == DataControlRowType.Header)
{
string mySortDirection = (Session[“mySortDirection”] != null ? Session[“mySortDirection”].ToString() : “”);
e.Row.Cells[cellIndex].CssClass += (mySortDirection == “ASC” ? ” sortascheader” : ” sortdescheader”);
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[cellIndex].CssClass += (e.Row.RowIndex % 2 == 0 ? ” sortaltrow” : “sortrow”);
}
break;
}
}
}
}
protected void gvwCountries_Sorting(object sender, GridViewSortEventArgs e)
{
Session[“mySortExpression”] = e.SortExpression;
DataView dataView = new DataView(GetMyDataTable());
dataView.Sort = e.SortExpression + ” ” + GetSortDirection(e.SortExpression);
gvwCountries.DataSource = dataView;
gvwCountries.DataBind();
ActivatePages();
}
#endregion
#region “Custom Functions”
#region “DataBind Events”
public DataTable GetMyDataTable()
{
string strConnection = ConfigurationManager.ConnectionStrings[“LoginCon”].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnection);
SqlCommand cmdUsers = new SqlCommand(“SP_GetSomeCountries”, myConnection);
cmdUsers.CommandType = CommandType.StoredProcedure;
parmCountries[0] = new SqlParameter(“@startRowIndex”, currentPageNumber);
parmCountries[1] = new SqlParameter(“@maximumRows”, PAGE_SIZE);
parmCountries[2] = new SqlParameter(“@totalRows”, SqlDbType.Int, 4);
parmCountries[2].Direction = ParameterDirection.Output;
if (txtCountries.Text.Trim() != “”)
{
parmCountries[3] = new SqlParameter(“@CountryName”, txtCountries.Text.Trim());
parmCount++;
}
for (int i = 0; i < parmCount; i++)
{
cmdUsers.Parameters.Add(parmCountries[i]);
}
SqlDataAdapter adapUsers = new SqlDataAdapter(cmdUsers);
adapUsers.Fill(dt);
Session[“totalRows”] = (int)cmdUsers.Parameters[“@totalRows”].Value;
return dt;
}
public void BindGrid()
{
gvwCountries.PageSize = PAGE_SIZE;
gvwCountries.DataSource = GetMyDataTable();
gvwCountries.DataBind();
if (Session[“totalRows”].ToString() != “0”)
{
ActivatePages();
}
}
#endregion
#region “Custom Sorting”
protected string GetSortDirection(string sortBy)
{
string sortDir = “ASC”;
if (ViewState[“sortBy”] != null)
{
string sortedBy = ViewState[“sortBy”].ToString();
if (sortedBy == sortBy)
{
sortDir = ” DESC”;
ViewState[“sortBy”] = null;
}
else
{
ViewState[“sortBy”] = sortBy;
}
}
else
{
ViewState[“sortBy”] = sortBy;
}
Session[“mySortDirection”] = sortDir;
return sortDir;
}
#endregion
#region “Custom Paging”
protected void ChangePage(object sender, CommandEventArgs e)
{
Session[“mySortDirection”] = “”;
Session[“mySortExpression”] = “”;
Label lblTotalPages = (Label)gvwCountries.BottomPagerRow.FindControl(“lblTotalPages”);
//Label lblCurrentPage = (Label)gvwCountries.BottomPagerRow.FindControl(“lblCurrentPage”);
DropDownList ddlPage = (DropDownList)gvwCountries.BottomPagerRow.FindControl(“ddlPages”);
currentPageNumber = (e.CommandName == “Previous” ? Int32.Parse(ddlPage.SelectedItem.Text) – 1 : Int32.Parse(ddlPage.SelectedItem.Text) + 1);
Session[“CurrentPageIndex”] = currentPageNumber;
BindGrid();
}
#endregion
#region “Display Pages”
public void ActivatePages()
{
GridViewRow gvrPager = gvwCountries.BottomPagerRow;
gvrPager.Visible = true;
double totalRows = Convert.ToInt32(Session[“totalRows”].ToString());
LinkButton Btn_Previous = (LinkButton)gvrPager.FindControl(“Btn_Previous”);
LinkButton Btn_Next = (LinkButton)gvrPager.FindControl(“Btn_Next”);
Label lblTotalPages = (Label)gvrPager.FindControl(“lblTotalPages”);
DropDownList ddlPage = (DropDownList)gvrPager.FindControl(“ddlPages”);
//Label lblCurrentPage = (Label)gvrPager.FindControl(“lblCurrentPage”);
lblTotalPages.Text = Convert.ToString(Math.Ceiling(totalRows / PAGE_SIZE));
for (int i = 1; i <= Convert.ToInt32(lblTotalPages.Text); i++)
{
ddlPage.Items.Add(new ListItem(i.ToString()));
}
//lblCurrentPage.Text = currentPageNumber.ToString();
ddlPage.Items.FindByText(currentPageNumber.ToString()).Selected = true;
if (currentPageNumber == 1)
{
Btn_Previous.Enabled = false;
Btn_Next.Enabled = (Int32.Parse(lblTotalPages.Text) > 1 ? true : false);
}
else
{
Btn_Previous.Enabled = true;
Btn_Next.Enabled = (currentPageNumber == Int32.Parse(lblTotalPages.Text) ? false : true);
}
}
#endregion
#endregion
protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl =(DropDownList)sender;
currentPageNumber = Convert.ToInt32(ddl.SelectedItem.Text);
BindGrid();
}
SP
ALTER PROCEDURE [SP_GetSomeCountries]
@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT
AS
DECLARE @first_id int
SET @startRowIndex = (@startRowIndex – 1) * @maximumRows + 1
IF @startRowIndex = 0
SET @startRowIndex = 1
SET ROWCOUNT @startRowIndex
SELECT @first_id = countryID FROM tbl_Countries
SET ROWCOUNT @maximumRows
SELECT countryID, countryName FROM tbl_Countries WHERE
(countryID >= @first_id )
SET ROWCOUNT 0
— GEt the total rows
SELECT @totalRows = COUNT(countryID) FROM tbl_Countries
StyleSheet
.tablestyle
{
font-family: arial;
font-size: small;
border: solid 1px #7f7f7f;
}
.altrowstyle
{
background-color: #edf5ff;
}
.headerstyle th
{
background: url(img/sprite.png) repeat-x 0px 0px;
border-color: #989898 #cbcbcb #989898 #989898;
border-style: solid solid solid none;
border-width: 1px 1px 1px medium;
color: #000;
padding: 4px 5px 4px 10px;
text-align: center;
vertical-align: bottom;
}
.headerstyle th a
{
font-weight: normal;
text-decoration: none;
text-align: center;
color: #000;
display: block;
padding-right: 10px;
}
.rowstyle .sortaltrow, .altrowstyle .sortaltrow
{
background-color: #edf5ff;
}
.rowstyle .sortrow, .altrowstyle .sortrow
{
background-color: #dbeaff;
}
.rowstyle td, .altrowstyle td
{
padding: 4px 10px 4px 10px;
border-right: solid 1px #cbcbcb;
}
.headerstyle .sortascheader
{
background: url(img/sprite.png) repeat-x 0px -100px;
}
.headerstyle .sortascheader a
{
background: url(img/dt-arrow-up.png) no-repeat right 50%;
}
.headerstyle .sortdescheader
{
background: url(img/sprite.png) repeat-x 0px -100px;
}
.headerstyle .sortdescheader a
{
background: url(img/dt-arrow-dn.png) no-repeat right 50%;
}
.pagerstyle
{
background: url(img/sprite.png) repeat-x 0px 0px;
border-color: #989898 #cbcbcb #989898 #989898;
border-style: solid solid solid none;
border-width: 1px 1px 1px medium;
color: #000;
padding: 4px 5px 4px 10px;
text-align: center;
vertical-align: bottom;
}
You must be logged in to post a comment.