Thursday 6 June 2013

Paging in Datalist Asp.net ,C#

Datalist Paging


Source Page  .aspx:


 <asp:DataList ID="DataListpost" runat="server" DataKeyField="id"
                      Width="100%"
            HorizontalAlign="Center">
  <ItemTemplate>
  </ItemTemplate>  </asp:DataList>
            
         


<asp:LinkButton id="Prev" Text="NewPost" OnClick="Page_DataList" runat="server"  
          ForeColor="#003366" />                
             <asp:LinkButton id="Next" Text="OldPost" runat="server" ForeColor="#003366" 
                          onclick="Page_DataList"  EnableViewState="true"  /> 


 <input type="hidden" id="PageSize" value="10" runat="server"/>
  <input type="hidden" id="CurrentPage" value="1" runat="server"/>
  <input type="hidden" id="TotalSize" runat="server"/>

Codebehind :

 SqlConnection con;
    SqlCommand cmd;

    DataTable dt;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["scon"].ConnectionString);
        if (!IsPostBack)
            BuildGrid();
      
        
    }

    protected void btnbook_Click(object sender, EventArgs e)
    {
        
    }
    public void BuildGrid()
    {
        cmd = new SqlCommand("select * from post order by id desc", con);

        con.Open();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        int startRecord = (int.Parse(CurrentPage.Value) - 1) *
       int.Parse(PageSize.Value);
        //Fetch only the necessary records.
        adp.Fill(ds, startRecord, int.Parse(PageSize.Value), "post");
        //DataBind the DataList    
        DataListpost.DataSource = ds.Tables["post"].DefaultView;
        DataListpost.DataBind();

        //Second Part
        //Create a new Command to select the total number of records
        SqlCommand myCmd = new SqlCommand("SELECT Count(*) from post", con);

        //retrieve the value
        TotalSize.Value = myCmd.ExecuteScalar().ToString();
        con.Close();
        BuildPagers();
    }
    public void BuildPagers()
    {
        //Check if its possible to have the previous page
        if ((int.Parse(CurrentPage.Value) - 1) <= 0)
        {
            Prev.Enabled = false;
        }
        else
        {
            Prev.Enabled = true;
        }
        //Check if its possible to have the next page  
        if ((int.Parse(CurrentPage.Value) * int.Parse(PageSize.Value))
        >= int.Parse(TotalSize.Value))
        {
            Next.Enabled = false;
        }
        else
        {
            Next.Enabled = true;
        }
    }
    public void Page_DataList(object sender, EventArgs e)
    {
        //Check for Button clicked
        if (((LinkButton)sender).ID == "Prev")
        {
            //Check if we are on any page greater than 0 
            if ((int.Parse(CurrentPage.Value) - 1) >= 0)
            {
                //Decrease the CurrentPage Value
                CurrentPage.Value = (int.Parse(CurrentPage.Value) - 1).ToString();
            }
        }
        else if (((LinkButton)sender).ID == "Next")
        {
            //Check if we can display the next page.
            if ((int.Parse(CurrentPage.Value) * int.Parse(PageSize.Value))
           < int.Parse(TotalSize.Value))
            {
                //Increment the CurrentPage value
                CurrentPage.Value = (int.Parse(CurrentPage.Value) + 1).ToString();
            }
        }
        //Rebuild the Grid
        BuildGrid();
       
    }

No comments:

Post a Comment