ListView - Paging issue - doesn't work from code behind

Last post 08-15-2008 2:08 PM by Mufasa245. 3 replies.

Sort Posts:

  • ListView - Paging issue - doesn't work from code behind

    02-18-2008, 5:07 AM
    • Loading...
    • ambuj
    • Joined on 12-02-2005, 5:38 PM
    • Posts 4

    Hello,

    I've got a ListView and a DataPager on my ASPX page.  I bind the ListView in my code behind:

                EGDataContext db = new EGDataContext();
                var ads = (from a in db.Ads
                          where a.SubCategoryId == subCategoryId
                          orderby a.PostedOn descending
                          select a);

                this.lvAds.DataSource = ads;
                this.lvAds.DataBind();

    My problem is that when I bind the ListView using the code above, my paging doesn't works correctly (pages don't change) but when I have a LinqDataSource on the ASPX page itself and bind the ListView using it's DataSourceID property and paging works correctly and as expected.

    My question is that is the paging supposed to work if I bind the ListView from code behind? And if it is, then what am I missing?

    Thanks in advance  
     

    Filed under: ,
  • Re: ListView - Paging issue - doesn't work from code behind

    02-19-2008, 10:30 PM
    Answer

    Hi ambuj ,

    I will write some codes in PagePropertiesChanging event of ListView.

     

            protected void lvItems_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
            {
                
                this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
                bind();
            }
    
            private void bind()
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True");
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [Region]", con);
                sda.Fill(ds);
                this.lvItems.DataSource = ds.Tables[0];
                lvItems.DataBind();
            }
     
    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: ListView - Paging issue - doesn't work from code behind

    02-25-2008, 12:35 AM
    • Loading...
    • Isystem
    • Joined on 02-25-2008, 4:31 AM
    • Posts 3

    Hi Samu,

    Is there anyother way to achieve this ? Actually, i am using Microsoft CRM  Webservice to retrieve data and using DataTable as DataSource to ListView. So, is there any other way to achive paging without calling bind() method again and again. In simple words, i want to implement data pager controle with ListView by using DataTable as a datasource. Currently, my paging is not working, pages don't change. Please help me to sort this out.

     

    Thanks in advance.

     

    Kind Regards,

    Sol 

     

     

  • Re: ListView - Paging issue - doesn't work from code behind

    08-15-2008, 2:08 PM
    • Loading...
    • Mufasa245
    • Joined on 05-19-2006, 12:38 PM
    • Columbus, OH
    • Posts 43
    As far as I know, and I'm not expert, no. You have to re-bind lots with the DataPager control. That control is typical Microsoft control with excessive binding. If you want high performance, you have to manually code the paging...

    Mufasa

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter