0
Answer

Paging with entity data framework

ashok kumar

ashok kumar

11y
1.3k
1
Aspx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentHome.aspx.cs" Inherits="EDTView.StudentHome" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 100%;
            border: 1px solid #000000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>
                    <div>
                        <div style="float: left; width: 65%; padding-left: 50px;">
                            &nbsp;&nbsp;</div>
                        <div style="float: left; margin-right: 15px; width: 30%;">
                            <asp:Label ID="lbluser" runat="server" Text=""></asp:Label></div>
                        <div style="clear: both;">
                            <div style="width: 50%; margin: 0 auto;">
                                <table class="style2">
                                    <tr>
                                        <td>
                                            &nbsp;
                                        </td>
                                        <td>
                                            &nbsp;
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Student Name
                                        </td>
                                        <td>
                                            <asp:Label ID="lblname" runat="server" Text=""></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Emailid
                                        </td>
                                        <td>
                                            <asp:Label ID="lblemailid" runat="server" Text=""></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Student Phone
                                        </td>
                                        <td>
                                            <asp:Label ID="lblphone" runat="server" Text=""></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            &nbsp;
                                        </td>
                                        <td>
                                            &nbsp;
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <asp:DataGrid ID="dtgridstudentresults" runat="server" AutoGenerateColumns="false">
                                                <Columns>
                                                    <asp:BoundColumn DataField="SUBJ_MATH" HeaderText="Math"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="SUBJ_PHY" HeaderText="Physic"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="SUBJ_CHEM" HeaderText="Chemistry"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_TOPPIC" HeaderText="Topic"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_TESTNAME" HeaderText="Test Name"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_TESTNATURE" HeaderText="Nature"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_MAXMARK" HeaderText="Max Marks"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_MARK" HeaderText="Mark"></asp:BoundColumn>
                                                    <asp:BoundColumn DataField="TEST_TESTDATE" HeaderText="Test Date"></asp:BoundColumn>
                                                </Columns>
                                                <HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" />
                                            </asp:DataGrid>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <table cellpadding="0" border="0" width="100%">
                                                <tr>
                                                    <td align="right">
                                                        <asp:LinkButton ID="lbtnFirst" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click">First</asp:LinkButton>
                                                        &nbsp;
                                                    </td>
                                                    <td align="right">
                                                        <asp:LinkButton ID="lbtnPrevious" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click">Previous</asp:LinkButton>&nbsp;&nbsp;
                                                    </td>
                                                    <td align="center" valign="middle">
                                                        <asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
                                                            OnItemDataBound="dlPaging_ItemDataBound">
                                                            <ItemTemplate>
                                                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                                                    CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>&nbsp;
                                                            </ItemTemplate>
                                                        </asp:DataList>
                                                    </td>
                                                    <td align="left">
                                                        &nbsp;&nbsp;<asp:LinkButton ID="lbtnNext" runat="server" CausesValidation="false"
                                                            OnClick="lbtnNext_Click">Next</asp:LinkButton>
                                                    </td>
                                                    <td align="left">
                                                        &nbsp;
                                                        <asp:LinkButton ID="lbtnLast" runat="server" CausesValidation="false" OnClick="lbtnLast_Click">Last</asp:LinkButton>
                                                    </td>
                                                    <td style="float: right;">
                                                        <asp:DropDownList ID="ddlindex" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlindex_SelectedIndexChanged">
                                                            <asp:ListItem Selected="True" Text="Index" Value="5"></asp:ListItem>
                                                            <asp:ListItem Selected="false" Text="10" Value="10"></asp:ListItem>
                                                            <asp:ListItem Selected="false" Text="15" Value="15"></asp:ListItem>
                                                            <asp:ListItem Selected="false" Text="20" Value="20"></asp:ListItem>
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan="5" align="center" style="height: 30px" valign="middle">
                                                        <asp:Label ID="lblPageInfo" runat="server"></asp:Label>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
            </table>
                            </div>
                        </div>
                    </div>
                </td>
             </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EWTModel;
using System.Data;

namespace EDTView
{
    public partial class StudentHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["data"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else if (!IsPostBack)
            {
                Student_Master student_master = new Student_Master();
                student_master = (Student_Master)Session["data"];
                lbluser.Text = student_master.Student_Name.ToString();
                bindstudentmarksdetails();
            }
        }
        public void bindstudentmarksdetails()
        {
            EWTEntities ob = new EWTEntities();
            var student_master = (Student_Master)Session["data"];
            var query = ob.Student_Master.Where(x => x.Student_id == student_master.Student_id).ToArray();
            if (query != null)
            {
                lblname.Text = query[0].Student_Name;
                lblemailid.Text = query[0].Student_Email;
                lblphone.Text = query[0].Student_Phone;
            }
            int intindex = Convert.ToInt32(ddlindex.SelectedValue);
            var testrecord = ob.STUDENT_TEST.Where(x => x.STUDENT_ID == student_master.Student_id && (x.TESTID>= (CurrentPage*intindex)+1
                && x.TESTID<=(CurrentPage+1)*intindex)).ToArray();
            dtgridstudentresults.DataSource = testrecord;
            dtgridstudentresults.DataBind();
            ViewState["TotalPages"] =Math.Ceiling(Convert.ToDecimal(ob.STUDENT_TEST.Where(x=>x.STUDENT_ID==student_master.Student_id).Count()/Convert.ToDecimal(ddlindex.SelectedValue)));
            this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + ViewState["TotalPages"].ToString();
            lbtnFirst.Enabled = CurrentPage!=0;
            lbtnPrevious.Enabled =CurrentPage!=0;
            lbtnLast.Enabled=CurrentPage+1 != Convert.ToInt32(ViewState["TotalPages"]);
            lbtnNext.Enabled = CurrentPage + 1 != Convert.ToInt32(ViewState["TotalPages"]);
            doPaging();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {


            CurrentPage += 1;
            this.bindstudentmarksdetails();

        }
        protected void lbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            this.bindstudentmarksdetails();

        }
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("Paging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                this.bindstudentmarksdetails();
              
            }
        }
        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Style.Add("fone-size", "14px");
                lnkbtnPage.Font.Bold = true;
            }
        }
        protected void lbtnLast_Click(object sender, EventArgs e)
        {


            CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
            this.bindstudentmarksdetails();


        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;
            this.bindstudentmarksdetails();
        }
        #region Private Properties
        private int CurrentPage
        {
            get
            {
                object objPage = ViewState["_CurrentPage"];
                int _CurrentPage = 0;
                if (objPage == null)
                {
                    _CurrentPage = 0;
                }
                else
                {
                    _CurrentPage = (int)objPage;
                }
                return _CurrentPage;
            }
            set { ViewState["_CurrentPage"] = value; }
        }
        private int fistIndex
        {
            get
            {


                int _FirstIndex = 0;
                if (ViewState["_FirstIndex"] == null)
                {
                    _FirstIndex = 0;
                }
                else
                {
                    _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
                }
                return _FirstIndex;
            }
            set { ViewState["_FirstIndex"] = value; }
        }
        private int lastIndex
        {
            get
            {


                int _LastIndex = 0;
                if (ViewState["_LastIndex"] == null)
                {
                    _LastIndex = 0;
                }
                else
                {
                    _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
                }
                return _LastIndex;
            }
            set { ViewState["_LastIndex"] = value; }
        }
        #endregion
        #region function for paging
        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");


            fistIndex = CurrentPage - 5;




            if (CurrentPage > 5)
            {
                lastIndex = CurrentPage + 5;
            }
            else
            {
                lastIndex = 10;
            }
            if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
            {
                lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
                fistIndex = lastIndex - 10;
            }


            if (fistIndex < 0)
            {
                fistIndex = 0;
            }


            for (int i = fistIndex; i < lastIndex; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }


            this.dlPaging.DataSource = dt;
            this.dlPaging.DataBind();
        }

        protected void ddlindex_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPage = 0;
            this.bindstudentmarksdetails();
        }
        #endregion
    }
}


dj
j