Thursday 13 September 2012

Set paging in datalist control in asp.net

Design Page

 <asp:DataList ID="dlPaging" runat="server" CellSpacing="15" RepeatColumns="2" Width="85%">
                        <ItemTemplate>
          <table>
                                <tr>
                                    <td >
                                        <h4>
            <asp:Label ID="Label6" runat="server" Text='<%#Eval("Column Name") %>'></asp:Label></h4>
                                    </td>
                                </tr>
           </table>
<div id="paging">
                     <asp:Label ID="lblCurrpage" runat="server" CssClass="labelpaging"></asp:Label>
                </div>
</ItemTemplate>
</asp:DataList>


Coading 

string m_pagingId;

protected void Page_Load(object sender, EventArgs e)
    {
    if (!string.IsNullOrEmpty(this.Request.QueryString["page"]))
        {
            m_pagingId = this.Request.QueryString["page"];
            Paging();
        }
     }

private void Paging()
    {
        string strQuery = "";
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        strQuery = string.Format("Select  Query");

        SqlCommand cmd = new SqlCommand(strQuery, cn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable DT = new DataTable();
        adp.Fill(DT);

        if (DT.Rows.Count == 0)
        {
            divpage.Visible = false;
            lblError.Text = " No Items to Show";
            return;
        }
        //if (Session["catID"] == null && Session["object"] == null) { return; }

        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = DT.DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 2;

        int curpage = 0;

        //when click paging, it sends the page index through query string
        if (m_pagingId != null)
        {
            curpage = Convert.ToInt32(m_pagingId);
        }
        else
        {
            curpage = 1;
        }
        pageds.CurrentPageIndex = curpage - 1;

        //display the current page index setup
        if (curpage == 1 && pageds.DataSourceCount < pageds.PageSize)
            lblCurrpage.Text = "Page:";
        else if (pageds.DataSourceCount == 0)
            lblCurrpage.Text = "No productst";
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
            lblCurrpage.Text = "Page: <a href='Current  Page Name.aspx?page=' style='text-decoration:none;' ></a>";
        else
            lblCurrpage.Text = "Page:";

        //set link for each index
        for (int i = 1; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
                lblCurrpage.Text = lblCurrpage.Text + " &nbsp; " + i.ToString();
            else
                lblCurrpage.Text = lblCurrpage.Text + " &nbsp; <a href='Current Page Name.aspx?page=" + i.ToString() + "' style='text-decoration:none;'>" + i.ToString() + "</a>";
        }

        //bind the value in datalist
        dlPaging.DataSource = pageds;
        dlPaging.DataBind();
    }

No comments:

Post a Comment