Wednesday 8 January 2014

HOw to Set ListView DataPager Page Index Property to specific Page Number in asp.net

Copy & Paste this on .aspx file :

 <asp:ListView ID="ListView1" runat="server" GroupItemCount="1" GroupPlaceholderID="groupPlaceHolder1"
                ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="ListView1_PagePropertiesChanging"
                OnItemDataBound="ListView1_ItemDataBound">
                <LayoutTemplate>
                    <table>
                        <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                    </table>
                </LayoutTemplate>
                <GroupTemplate>
                    <tr>
                        <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                    </tr>
                </GroupTemplate>
                <ItemTemplate>
                    <table align="center" cellpadding="0" cellspacing="0" width="730px">
                        <tr>
                            <td width="64px" height="80px" >
                                <asp:Image ID="imgtick" runat="server" ImageUrl="~/Image/tick.png" Height="35px"
                                    Width="35px" ImageAlign="AbsMiddle" /><asp:Label ID="lblid" runat="server" Text='<%# Bind("ID") %>'
                                        Visible="false"></asp:Label>
                            </td>
                            <td width="654px" valign="middle">
                                <table align="center" cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td width="110px" height="60px" align="left" valign="middle" style="border-right-style: solid;
                                            border-right-width: 1px; border-right-color: #CCCCCC">
                                            
                                                <asp:Image ID="Image2" runat="server" Height="50px" Width="100px" ImageUrl='<%#Eval("AdImage","../{0}")%>' />
                                        </td>
                                        <td width="224px" height="60px" align="left" valign="top" style="padding-left: 5px;
                                            padding-right: 5px; word-wrap: break-word; border-right-style: solid; border-right-width: 1px;
                                            border-right-color: #CCCCCC; font-family: Arial; font-size: 9pt;">
                                            <span style="font-weight: bold">Title :</span>
                                                style="text-decoration: none; color: #023E7C"><%# Eval("Title")%><br />
                                            <span style="font-weight: bold">About : </span>
                                                style="text-decoration: none; color: Gray">
                                                <%# Eval("Description").ToString().Substring(0,70)%>
                                               
                                        </td>
                                        <td width="100px" align="center" valign="middle">
                                            <asp:Label ID="lblstatus" runat="server" Font-Bold="true" />
                                        </td>
                                        <td width="80px" align="center" valign="middle">
                                            <%# Eval("ActivationDate").ToString().Substring(0,9)%>
                                        </td>
                                        <td width="80px" align="center" valign="middle" style="border-right-style: solid;
                                            border-right-width: 1px; border-right-color: #CCCCCC">
                                            <%# Eval("ExpiryDate").ToString().Substring(0,9)%>
                                        </td>
                                        <td width="60px" align="center">
                                            <asp:Label ID="lblreward" runat="server" Text='<%# Bind("Reward")%>'></asp:Label>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td width="12px" >
                                &nbsp;
                            </td>
                        </tr>
                    </table>
                    </td>
                </ItemTemplate>
            </asp:ListView>
            <br />
<%-- DataPager---%>
            <asp:DataPager runat="server" ID="DataPager1" PageSize="8" PagedControlID="ListView1">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowPreviousPageButton="false"
                        ShowLastPageButton="false" />
                    <asp:NumericPagerField ButtonType="Button" />
                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False"
                        ShowPreviousPageButton="False" />
                  
                </Fields>
            </asp:DataPager>


Write this code on pageload event with isPostBack property false:

  protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    _cMasterPageName = Page.MasterPageFile;
                    if (Session["pageno"] != null) /// set datapager to page 2
                        DataPager1.SetPageProperties(Convert.ToInt32(Session["pageno"]), 8, false);
                    fillListView();                  
                    getusertype();
                }
            }
            catch (Exception)
            {
                
                ////throw;
            }
           
        }


         protected void fillListView() /// fill listview
        {          
            con.Open();
            SqlDataAdapter da= new SqlCommand("Select * from UserTable",con)
            DataTable dt = new DataTable();
            da.Fill(dt);
            ListView1.DataSource = dt;
            ListView1.DataBind();
        }


protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            try
            {
                DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
                Session["pageno"] = e.MaximumRows; // save page size
                fillListView();
            }
            catch (Exception)
            {
                
                ////throw;
            }
            
        }

No comments:

Post a Comment