CUSTOM Paging In GridView
Code on GridView
<asp:GridView ID="GridView1" AutoGenerateColumns="false" CssClass="table table-striped"
EmptyDataText="No record found!" runat="server">
<Columns>
<asp:TemplateField HeaderText="S.No.">
<ItemTemplate>
<asp:Label ID="lblR_Number" runat="server" Text='<%#Eval("R_NUMBER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Registration No">
<ItemTemplate>
<asp:Label ID="lblREGISTRATION_NO" runat="server" Text='<%#Eval("REGISTRATION_NO") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vehicle Name">
<ItemTemplate>
<asp:Label ID="lblVEHICLE_NAME" runat="server" Text='<%#Eval("VEHICLE_NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manufacturer">
<ItemTemplate>
<asp:Label ID="lblMANUFACTURER" runat="server" Text='<%#Eval("MANUFACTURER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pollution Expiry">
<ItemTemplate>
<asp:Label ID="lblPOLLUTION" runat="server" Text='<%#Eval("POLLUTION") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vehicle Type">
<ItemTemplate>
<asp:Label ID="lblType" runat="server" Text='<%#Eval("VEHICLE_TYPE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Capacity">
<ItemTemplate>
<asp:Label ID="lblCAPACITY" runat="server" Text='<%#Eval("CAPACITY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Details">
<ItemTemplate>
<a href="javascript:void(0);" class="tip" onclick="View('<%#Eval("VEHICLE_ID") %>')">
<i class="icon-book"></i></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<a href="javascript:void(0);" class="tip" onclick="btnclick1('<%#Eval("VEHICLE_ID") %>')">
<i class="icon-edit"></i></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<a class="icon-remove confirm" href="#" onclick="$('#hid').val('<%#Eval("VEHICLE_ID") %>');">
</a>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Assign Route">
<ItemTemplate>
<a href="assign-vehicle-route.aspx?v=<%#Eval("VEHICLE_ID") %>" class="tip"><i class="icon-road">
</i></a>
</ItemTemplate>
</asp:TemplateField>--%>
<%--<asp:TemplateField HeaderText="Manage">
<ItemTemplate>
<a href="#" class="dropdown-toggle navbar-icon" data-toggle="dropdown"><i class="icon-cog">
</i></a>
<ul class="dropdown-menu pull-right">
<li><a href="#"><i class="icon-plus"></i>Add new option</a></li>
<li><a href="#"><i class="icon-reorder"></i>View statement</a></li>
<li><a href="#"><i class="icon-cogs"></i>Parameters</a></li>
</ul>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
<div class="well-smoke body">
<asp:Label ID="lblPage" CssClass="dataTables_info" runat="server" Text=""></asp:Label>
<div class="pagination pagination-right">
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton" runat="server" Text='<%#Eval("Text")%>' CommandArgument='<%#Eval("Value")%>'
Enabled='<%#Eval("Enabled")%>' OnClick="Page_Changed">
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</div>
CODE OF C#
{
try
{
string[] _strSession = Session["COLLEGEADMIN"].ToString().Split(';');
_p = new p_vehicle();
_d = new d_transKshitij();
ds = new DataSet();
_p.College_id = _strSession[1].ToString();
int totalRows = 0;
_p.PageIndex = pageIndex;
_p.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
_p.Vehiclename = !string.IsNullOrEmpty(txtVehicle.Text) ? txtVehicle.Text.Trim() : string.Empty;
ds = _d.fnkGetvehicledetails(_p);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
totalRows = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString().Trim());
//lblTotalEnquiry.Text = totalRows.ToString();
Utilities.PopulatePager(totalRows, pageIndex, GridView1.Rows.Count, ddlPageSize, Repeater1, lblPage);
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
//lblTotalEnquiry.Text = "0";
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "error('Something went wrong! Please try again.');", true);
}
}
protected void PageSize_Changed(object sender, EventArgs e)
{
this.fnGridBind(1);
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "fun();", true);
}
protected void Page_Changed(object sender, EventArgs e)
{
try
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.fnGridBind(pageIndex);
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "fun();", true);
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "error('Something went wrong! Please try again.');", true);
}
}
Paging function
public static void PopulatePager(int recordCount, int currentPage, int gridRowCount, DropDownList drpPageSize, Repeater rptPager, Label lblPage)
{
try
{
double dblPageCount = (double)((decimal)recordCount / decimal.Parse(drpPageSize.SelectedValue));
int pageCount = (int)Math.Ceiling(dblPageCount);
List<ListItem> pages = new List<ListItem>();
int _cnt = 0;
if (pageCount > 0)
{
pages.Add(new ListItem("First", "1", currentPage > 1));
if (currentPage + 1 < pageCount)
{
for (int i = 1; i <= currentPage + 5; i++)
{
if (pageCount > i && currentPage - 2 <= i)
{
if (_cnt <= 5)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
_cnt = _cnt + 1;
}
}
}
pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
}
else
{
for (int i = 1; i <= pageCount; i++)
{
if (pageCount - 5 < i)
{
if (_cnt <= 5)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
_cnt = _cnt + 1;
}
}
}
pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
}
}
int _f = Convert.ToInt16(drpPageSize.SelectedValue.ToString()) - gridRowCount;
int _t = currentPage * (Convert.ToInt16(drpPageSize.SelectedValue.ToString())) - _f;
//_f = _t - (Convert.ToInt16(drpPageSize.SelectedValue.ToString())) + 1;
_f = (Convert.ToInt16(drpPageSize.SelectedValue) * (currentPage - 1)) + 1;
//lblPage.Text = Convert.ToString(_f) + " - " + Convert.ToString(_t) + " of " + Convert.ToString(recordCount);
if (recordCount > 0)
{
lblPage.Visible = true;
lblPage.Text = Convert.ToString(_f) + " - " + Convert.ToString(_t) + " of " + Convert.ToString(recordCount);
rptPager.Visible = true;
rptPager.DataSource = pages;
rptPager.DataBind();
}
else
{
lblPage.Visible = false;
rptPager.Visible = false;
rptPager.DataSource = null;
rptPager.DataBind();
}
}
catch (Exception e)
{ }
finally
{ }
}
PROCEDURE THAT IS USED IN PAGING
CREATE PROC [dbo].[GET_COLLEGE_VEHICLE]
(
@COLLEGE_ID NVARCHAR(100)='',
@PAGE_INDEX INT = 0,
@PAGE_SIZE INT = 0,
@TOTAL_ROWS INT=0 OUTPUT,
@VEHICLE NVARCHAR(100)=NULL
)
AS
DECLARE @START_ROW_INDEX INT
DECLARE @END_ROW_INDEX INT
SET @START_ROW_INDEX = ((@PAGE_INDEX-1)*@PAGE_SIZE)+1
SET @END_ROW_INDEX = @PAGE_INDEX*@PAGE_SIZE
SELECT ROW_NUMBER() OVER (ORDER BY T1.VEHICLE_ID) AS R_NUMBER,
T1.VEHICLE_ID,T1.VEHICLE_NAME,T1.REGISTRATION_NO,T1.MANUFACTURER,T1.VEHICLE_TYPE,
(CASE T1.CAPACITY WHEN '' THEN 'N/A' ELSE T1.CAPACITY END)AS CAPACITY,ISNULL(CONVERT(NVARCHAR(20),T2.EXPIRY_DATE,106),'N/A') AS POLLUTION
INTO #RESULTSET
FROM COLLEGE_VEHICLE AS T1
LEFT JOIN COLLEGE_VEHICLE_POLLUTION AS T2
ON T2.VEHICLE_ID=T1.VEHICLE_ID AND T2.ACTIVE_FLAG='Y' AND T2.COLLEGE_ID=@COLLEGE_ID AND GETDATE() BETWEEN T2.START_DATE AND T2.EXPIRY_DATE
WHERE T1.ACTIVE_FLAG='Y' AND T1.COLLEGE_ID=@COLLEGE_ID AND (T1.VEHICLE_NAME LIKE '%'+@VEHICLE+'%' OR T1.REGISTRATION_NO LIKE '%'+@VEHICLE+'%')
SELECT R_NUMBER,VEHICLE_ID,VEHICLE_NAME,REGISTRATION_NO,MANUFACTURER,VEHICLE_TYPE,CAPACITY,POLLUTION
FROM #RESULTSET
WHERE R_NUMBER >= @START_ROW_INDEX AND R_NUMBER <= @END_ROW_INDEX
SELECT @TOTAL_ROWS = COUNT(*) FROM #RESULTSET
DROP TABLE #RESULTSET
No comments:
Post a Comment