Tuesday 2 July 2013

How To Create Paging or Custom Paging In DataList Control in Asp.Net



What is Paging:-

When a program is selected for execution, the system brings it into virtual storage, divides it into pages of four kilobytes, transfers the pages into central storage for execution. This dividing of pages is also known as Paging.

For Ex:-

       we all know very well GridView provide In Built Facility while an DataList can not provide In Built Facility so we have to create manually paging in datalist control.

Here is a code for creating a paging in DataList with PageSize is six 

void doPaging()

    {

        pagedData.DataSource = getTheData().DefaultView;

        pagedData.AllowPaging = true;

        pagedData.PageSize = 6;
    }

in this Example we can provide Next and Previous Button and Define size =6 Every that means only Six Record is Displayed per page.

when click on next button then the next six record will be displayed and click on previous then previous six record will be displayed.

Here is some link that is very useful for same programming like :-

to show Example of insert,update,delete in gridview using LINQ please click here insert,update,delete using Linq

How to Send SMS in asp.net Send SMS to user
 
upload file in database and bind to gridview Upload File and bind to Gridview

Example of Auto Complete TextBox Search Autocomplete TextBox in Ajax






apple.aspx:-

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
                    onitemdatabound="DataList1_ItemDataBound"Width="373px" BackColor="White"
                    BorderColor="#336666" BorderStyle="Double"BorderWidth="3px" CellPadding="10"
                    Font-Bold="True" Font-Italic="False" Font-Overline="False"
                    Font-Strikeout="False" Font-Underline="False"ForeColor="#99CCFF"
                    GridLines="Horizontal" AllowPaging="True"PageSize="1"
                  DataKeyField="id">
              
                    <FooterStyle BackColor="White" ForeColor="#333333"/>
                    <ItemStyle BackColor="White" ForeColor="#333333" />
                    <SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#336666" Font-Bold="True"ForeColor="White" />
                               <ItemTemplate>
                               <asp:HiddenField Value='<%# Eval("image")%>' ID="HiddenField1" runat="server" />
                               <asp:HiddenField Value='<%#Eval("productname"%>' ID="HiddenField2" runat="server" />
             <asp:HiddenField Value='<%# Eval("prize"%>'ID="HiddenField3" runat="server" />
                   <div style="border:solid 1px blue;text-decoration:blink"> <asp:Image ID="Image1" runat="server"AlternateText="hi" Width="230" Height="200"   />
                    <div id="overlay" class="web_dialog_overlay"></div>
<div id="dialog" class="web_dialog">
   <table style="width100%border0px;" cellpadding="3"cellspacing="0">
      <tr>
         <td class="web_dialog_title">Online Survey</td>
         <td class="web_dialog_title align_right">
            <input type="button"  id="btnclose" value="close"onclick="HideDialog();"  />
         </td>
      </tr>
      <tr>
      <td>
      <div id="print" style="font-sizemediumcolor#00FF00text-decorationblink;  marginautopaddinginherittable-layoutauto;border-collapseseparateborder-spacinginherit"></div>
      </td>
      </tr>
      </table>
</div>
            <br /><%# Eval("prize"%> <br />
                       <asp:LinkButton ID="LinkButton1" runat="server"OnClientClick="return Enterno();" OnClick="add" CommandArgument='<%#Eval("id"%>'>Add To Cart</asp:LinkButton>
                       &nbsp;&nbsp&nbsp;&nbsp <input type="button" onclick="ShowDialog('<%# Eval("id"%>    ')" value="show Feature" />
                     
                     
                     
</div>
                     
         </ItemTemplate>
       
       
                </asp:DataList>
                <center>
                <asp:ImageButton ID="prev" Width="100" Height="60"runat="server" ImageUrl="~/image/peevious.jpeg" OnClick="btnprev_Click"/>
        <asp:Label ID="lblCurrentPage" runat="server" Font-Bold="True"ForeColor="#FFFF66"></asp:Label>


    <asp:ImageButton ID="next" Width="100" Height="60" runat="server"ImageUrl="~/image/next.jpeg" OnClick="btnnext_Click" />
  
    </center>
    

apple.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class apple : System.Web.UI.Page
{
    PagedDataSource pagedData = new PagedDataSource();
    int CurPage = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        doPaging();

    }

    public DataTable getTheData()
    {
        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

        string q1 = "select * from nokia where category = 'apple'";

        DataSet ds = new DataSet();
        //SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection cn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(q1, cn);

        // SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

        SqlDataAdapter objSQLAdapter = new SqlDataAdapter(q1, cn); objSQLAdapter.Fill(ds, "nokia");


        return ds.Tables[0];

    }

    void doPaging()
    {

        pagedData.DataSource = getTheData().DefaultView;

        pagedData.AllowPaging = true;
        pagedData.PageSize = 6;



        try
        {
            if (Request["Page"].ToString() != null)
            {
                CurPage = Int32.Parse(Request["Page"].ToString());
            }
            else
            {
                CurPage = 1;
            }

            pagedData.CurrentPageIndex = CurPage - 1;

        }

        catch

        (Exception ex)
        {

            pagedData.CurrentPageIndex = 0;

        }


        lblCurrentPage.Text = "Page: " + CurPage.ToString() + " of " + pagedData.PageCount.ToString(); ;

        DataList1.DataSource = pagedData;
        DataList1.DataBind();

    }


    protected void btnprev_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage - 1));
    }

    protected void btnnext_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage + 1));
    }

    protected void bsrc_Click(object sender, EventArgs e)
    {
        //Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage + 1));
    }


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HiddenField hf = e.Item.FindControl("HiddenField1"asHiddenField;
        if (hf != null)
        {
            string val = hf.Value;

            Image img = e.Item.FindControl("Image1"as Image;
            img.ImageUrl = "~/" + val;
        }


    }


   

}

0 comments:

Post a Comment