locked
list view page nation RRS feed

  • Question

  • User616860969 posted

    <div> i have 2 categories LIKE cat1 and Cat2 , first time page load i bind all data to list view , and after i bind to data based on category id</div> <div>my problem is i put page size is 5 , and total i have 12 records ,cat1 is 7 and cat2 is 5,first time page load i show the all data ,here i got 3 page index i click on 3page index its load the recored now i am select any category like cat1 or cat2  its show the pagenation is 3pageindex and dont show the first page????</div>

    aspX:
    <asp:HiddenField ID="filterconacts" runat="server" />

    <div>
    <div class="sub-filter-wrap">
    <asp:Panel ID="pnlFilters" runat="server">
    <ul class="filters-wrap">
    <span class="say-type">Filter By : </span>
    <li filterby="All" runat="server">
    <asp:LinkButton ID="lbAll" runat="server" Text="All" OnClick="lbAll_Click"></asp:LinkButton></li>
    <asp:Repeater ID="repeatFilters" runat="server">
    <ItemTemplate>
    <li class="contacts" filterby='<%# Eval("CategoryID")%>' runat="server">
    <asp:LinkButton ID="lbFilterCategory" runat="server" Text='<%# Eval("CategoryName")%>' OnCommand="lbFilterCategory_Command" CommandName="filter" CommandArgument='<%# Eval("CategoryID")%>'></asp:LinkButton>
    </li>
    </ItemTemplate>
    </asp:Repeater>
    </ul>
    </asp:Panel>
    </div>
    <div class="content-right-wrap ph-container-margins">
    <asp:ListView ID="lstViewContacts" runat="server" GroupItemCount="1" OnPagePropertiesChanging="lstViewContacts_PagePropertiesChanging">
    <grouptemplate>
    <div id="itemPlaceholderContainer" runat="server" class="row">
    <div id="itemPlaceholder" runat="server"></div>
    </div>
    </grouptemplate>
    <itemtemplate>




    <%# Eval("Name") %>
    <%# Eval("Phone") %>


    </itemtemplate>

    <layouttemplate>
    <div>
    <div id="groupPlaceholderContainer" runat="server">
    <div id="groupPlaceholder" runat="server"></div>
    </div>
    </div>

    </layouttemplate>
    </asp:ListView>
    </div>
    <table width="100%">
    <tr>
    <td colspan="6" class="paginator">

    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lstViewContacts" PageSize="3">

    <Fields>

    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="false" ShowNextPageButton="false" />
    <asp:NumericPagerField ButtonCount="5" />
    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="true" ShowNextPageButton="true"
    ShowPreviousPageButton="false" />

    </Fields>
    </asp:DataPager>
    </td>
    </tr>
    </table>

    </div>
    </div>


    <script>

    var selectedfilterby = $("input[id$='hfFilterby']").val();
    $(".filters-wrap > li[filterby='" + selectedfilterby + "']").addClass("seletedfilterby");


    });

    </script>

    code:

    public partial class DesktopModules_ViewContactList : PortalModuleBase
    {
    SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings["SiteSqlServer"]);
    Data data = new Data();

    #region Constants
    public string availableTags;
    #endregion
    #region properties
    public int CategoryId
    {
    get
    {
    if (ViewState["CategoryId"] != null)
    {
    return int.Parse(ViewState["CategoryId"].ToString());
    }
    else if (Request.QueryString["CategoryId"] != null)
    {
    return int.Parse(Request.QueryString["CategoryId"].ToString());
    }
    else
    return 0;
    }
    set
    {
    ViewState["CategoryId"] = value;

    }
    }
    #endregion
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {

    //hfFilterby.Value = "All";
    bindAllcontacts();
    if (!IsPostBack)
    {
    hfFilterby.Value = "All";

    BindData();

    }

    }


    public void filtercontactsList()
    {
    //Get contacts based on categoryid
    if (!string.IsNullOrEmpty(filterconacts.Value))
    {
    DataSet contactsList = data.GetContactsByCategoryId(sqlConnection, int.Parse(filterconacts.Value));

    lstViewContacts.DataSource = contactsList;

    lstViewContacts.DataBind();
    }
    }

    protected void lstViewContacts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {

    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    if (!string.IsNullOrEmpty(filterconacts.Value))
    {
    filtercontactsList();

    }
    else
    {
    bindAllcontacts();
    }
    }

    protected void lbFilterCategory_Command(object sender, CommandEventArgs e)
    {

    if (e.CommandName == "filter")
    {
    //Get contacts based on categoryid
    DataSet contactsList = data.GetContactsByCategoryId(sqlConnection, int.Parse(e.CommandArgument.ToString()));
    hfFilterby.Value = e.CommandArgument.ToString();
    filterconacts.Value = e.CommandArgument.ToString();
    lstViewContacts.DataSource = contactsList;

    lstViewContacts.DataBind();

    }
    }

    public void bindAllcontacts()
    {
    hfFilterby.Value = "All";
    //Get All contacts based on parent categoryid
    DataSet contactsList = data.GetContactsFromAllChildCategories(sqlConnection, CategoryId);
    lstViewContacts.DataSource = contactsList;
    lstViewContacts.DataBind();
    }

    protected void lbAll_Click(object sender, EventArgs e)
    {
    //hfFilterby.Value = "All";

    bindAllcontacts();
    }





    #region Methods

    private void BindData()
    {

    bindAllcontacts();

    }
    #endregion
    }

    Tuesday, September 6, 2016 11:58 PM

Answers

  • User36583972 posted

    Hi sanjaykumar pushadapu,

    According to your description, you should re-assignment DataPager's SetPageProperties value when you re-bind the data source. I have made a sample on my side. The following code is for your reference.

    HTML:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body
            {
                font-family: Arial;
                font-size: 10pt;
            }
            table
            {
                border: 1px solid #ccc;
            }
            table th
            {
                background-color: #F7F7F7;
                color: #333;
                font-weight: bold;
            }
            table th, table td
            {
                padding: 5px;
                border-color: #ccc;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
            ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            Time
                        </th>
                         <th>
                            Count
                        </th>
                    </tr>
                    <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                    <tr>
                        <td colspan="3">
                            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="4" >
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                        ShowNextPageButton="false" />
                                    <asp:NumericPagerField ButtonType="Link" />
                                    <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false"
                                        ShowPreviousPageButton="false" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <GroupTemplate>
                <tr>
                    <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                </tr>
            </GroupTemplate>
            <ItemTemplate>
                <td>
                    <%# Eval("ID") %>
                </td>
                <td>
                    <%# Eval("Name") %>
                </td>
                <td>
                    <%# Eval("Time") %>
                </td>
                 <td>
                    <%# Eval("Count") %>
                </td>
            </ItemTemplate>
        </asp:ListView>
            <asp:Button ID="Button1" runat="server" Text="filter(ID>2)>" OnClick="Button1_Click" />
        </form>
    </body>

    Code Behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class CS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataColumn dc1 = new DataColumn("ID", typeof(Int32));
                DataColumn dc2 = new DataColumn("Name", typeof(string));
                DataColumn dc3 = new DataColumn("Time", typeof(int));
                DataColumn dc4 = new DataColumn("Count", typeof(int));
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
    
                DataRow row = dt.NewRow();
                row["ID"] = 1;
                row["Name"] = "BeiJing";
                row["Time"] = 100;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 2;
                row["Name"] = "New York";
                row["Time"] = 200;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 3;
                row["Name"] = "Shang Hai";
                row["Time"] = 300;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 4;
                row["Name"] = "London";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 5;
                row["Name"] = "Korea";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 6;
                row["Name"] = "Japan";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 7;
                row["Name"] = "America";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 8;
                row["Name"] = "Taipie";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
                this.BindListView();
            }
        }
    
        static DataTable dt = new DataTable("data");
        private void BindListView()
        {
            lvCustomers.DataSource = dt;
            lvCustomers.DataBind();
        }
    
        protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            this.BindListView();
        }
    
        static DataTable dd = new DataTable("newdatatable");
        private void rebind()
        {
            DataColumn dc1 = new DataColumn("ID", typeof(Int32));
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            DataColumn dc3 = new DataColumn("Time", typeof(int));
            DataColumn dc4 = new DataColumn("Count", typeof(int));
            dd.Columns.Add(dc1);
            dd.Columns.Add(dc2);
            dd.Columns.Add(dc3);
            dd.Columns.Add(dc4);
            DataRow[] drs = dt.Select("ID>'2'");
    
            foreach (var item in drs)
            {
                DataRow row = dd.NewRow();
                row["ID"] = item[0];
                row["Name"] = item[1];
                row["Time"] = item[2];
                row["Count"] = item[3];
                dd.Rows.Add(row);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            rebind();
            lvCustomers.DataSource = dd;
            lvCustomers.DataBind();
            (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(0, 4, true);
        }
    }
    

    ScreenShot:

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 6:43 AM
  • User-1642217485 posted

    hi  sanjaykumar pushadapu,

    According to the above error message, result from the filter method has been executed one time ,data has filled in the datatable . so the second time you click, it will start again filled,  error will come out .

    After my test , make the following change to the yohann lu's code , it will run perfect .

    Changed code:

     private void rebind()
            {
                DataTable dd = new DataTable("newdatatable");
                DataColumn dc1 = new DataColumn("ID", typeof(Int32));
                DataColumn dc2 = new DataColumn("Name", typeof(string));
                DataColumn dc3 = new DataColumn("Time", typeof(int));
                DataColumn dc4 = new DataColumn("Count", typeof(int));
                dd.Columns.Add(dc1);
                dd.Columns.Add(dc2);
                dd.Columns.Add(dc3);
                dd.Columns.Add(dc4);
                DataRow[] drs = dt.Select("ID>'2'");
    
                foreach (var item in drs)
                {
                    DataRow row = dd.NewRow();
                    row["ID"] = item[0];
                    row["Name"] = item[1];
                    row["Time"] = item[2];
                    row["Count"] = item[3];
                    dd.Rows.Add(row);
                }
                lvCustomers.DataSource = dd;
                lvCustomers.DataBind();
                (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(0, 4, true);
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                rebind();
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2016 3:06 PM

All replies

  • User36583972 posted

    Hi sanjaykumar pushadapu,

    According to your description, you should re-assignment DataPager's SetPageProperties value when you re-bind the data source. I have made a sample on my side. The following code is for your reference.

    HTML:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body
            {
                font-family: Arial;
                font-size: 10pt;
            }
            table
            {
                border: 1px solid #ccc;
            }
            table th
            {
                background-color: #F7F7F7;
                color: #333;
                font-weight: bold;
            }
            table th, table td
            {
                padding: 5px;
                border-color: #ccc;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
            ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            Time
                        </th>
                         <th>
                            Count
                        </th>
                    </tr>
                    <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                    <tr>
                        <td colspan="3">
                            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="4" >
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                        ShowNextPageButton="false" />
                                    <asp:NumericPagerField ButtonType="Link" />
                                    <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false"
                                        ShowPreviousPageButton="false" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <GroupTemplate>
                <tr>
                    <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                </tr>
            </GroupTemplate>
            <ItemTemplate>
                <td>
                    <%# Eval("ID") %>
                </td>
                <td>
                    <%# Eval("Name") %>
                </td>
                <td>
                    <%# Eval("Time") %>
                </td>
                 <td>
                    <%# Eval("Count") %>
                </td>
            </ItemTemplate>
        </asp:ListView>
            <asp:Button ID="Button1" runat="server" Text="filter(ID>2)>" OnClick="Button1_Click" />
        </form>
    </body>

    Code Behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class CS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataColumn dc1 = new DataColumn("ID", typeof(Int32));
                DataColumn dc2 = new DataColumn("Name", typeof(string));
                DataColumn dc3 = new DataColumn("Time", typeof(int));
                DataColumn dc4 = new DataColumn("Count", typeof(int));
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
    
                DataRow row = dt.NewRow();
                row["ID"] = 1;
                row["Name"] = "BeiJing";
                row["Time"] = 100;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 2;
                row["Name"] = "New York";
                row["Time"] = 200;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 3;
                row["Name"] = "Shang Hai";
                row["Time"] = 300;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 4;
                row["Name"] = "London";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 5;
                row["Name"] = "Korea";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 6;
                row["Name"] = "Japan";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 7;
                row["Name"] = "America";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
    
                row = dt.NewRow();
                row["ID"] = 8;
                row["Name"] = "Taipie";
                row["Time"] = 400;
                row["Count"] = 100;
                dt.Rows.Add(row);
                this.BindListView();
            }
        }
    
        static DataTable dt = new DataTable("data");
        private void BindListView()
        {
            lvCustomers.DataSource = dt;
            lvCustomers.DataBind();
        }
    
        protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            this.BindListView();
        }
    
        static DataTable dd = new DataTable("newdatatable");
        private void rebind()
        {
            DataColumn dc1 = new DataColumn("ID", typeof(Int32));
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            DataColumn dc3 = new DataColumn("Time", typeof(int));
            DataColumn dc4 = new DataColumn("Count", typeof(int));
            dd.Columns.Add(dc1);
            dd.Columns.Add(dc2);
            dd.Columns.Add(dc3);
            dd.Columns.Add(dc4);
            DataRow[] drs = dt.Select("ID>'2'");
    
            foreach (var item in drs)
            {
                DataRow row = dd.NewRow();
                row["ID"] = item[0];
                row["Name"] = item[1];
                row["Time"] = item[2];
                row["Count"] = item[3];
                dd.Rows.Add(row);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            rebind();
            lvCustomers.DataSource = dd;
            lvCustomers.DataBind();
            (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(0, 4, true);
        }
    }
    

    ScreenShot:

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 6:43 AM
  • User616860969 posted

    thank you Yohann lu

    Monday, September 12, 2016 9:13 AM
  • User616860969 posted

    sorry yohann lu i got error 

    A column named 'ID' already belongs to this DataTable.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.DuplicateNameException: A column named 'ID' already belongs to this DataTable.

    Source Error:

    Line 17:             DataColumn dc3 = new DataColumn("Time", typeof(int));
    Line 18:             DataColumn dc4 = new DataColumn("Count", typeof(int));
    Line 19:             dt.Columns.Add(dc1);
    Line 20:             dt.Columns.Add(dc2);
    Line 21:             dt.Columns.Add(dc3);
    Monday, September 12, 2016 9:37 AM
  • User616860969 posted

    hi yohann lu

    this error comes when click button second time ,

    thank you

    Monday, September 12, 2016 9:41 AM
  • User-1642217485 posted

    hi  sanjaykumar pushadapu,

    According to the above error message, result from the filter method has been executed one time ,data has filled in the datatable . so the second time you click, it will start again filled,  error will come out .

    After my test , make the following change to the yohann lu's code , it will run perfect .

    Changed code:

     private void rebind()
            {
                DataTable dd = new DataTable("newdatatable");
                DataColumn dc1 = new DataColumn("ID", typeof(Int32));
                DataColumn dc2 = new DataColumn("Name", typeof(string));
                DataColumn dc3 = new DataColumn("Time", typeof(int));
                DataColumn dc4 = new DataColumn("Count", typeof(int));
                dd.Columns.Add(dc1);
                dd.Columns.Add(dc2);
                dd.Columns.Add(dc3);
                dd.Columns.Add(dc4);
                DataRow[] drs = dt.Select("ID>'2'");
    
                foreach (var item in drs)
                {
                    DataRow row = dd.NewRow();
                    row["ID"] = item[0];
                    row["Name"] = item[1];
                    row["Time"] = item[2];
                    row["Count"] = item[3];
                    dd.Rows.Add(row);
                }
                lvCustomers.DataSource = dd;
                lvCustomers.DataBind();
                (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(0, 4, true);
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                rebind();
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2016 3:06 PM
  • User616860969 posted

    thank you sir

    Wednesday, September 14, 2016 4:37 AM