title: Why page number is not enabled correctly in listview and datapager
body: <p>I am using listview and datapager for display. I use two level of listview, where inner listview item can be 6 types. I use my own datasource. I only need to display 1 inner listviewitem per page. It display each page correctly, but page number is not set right, eg, when 2nd page (other as well) is click, 2nd page number is not disabled while 1st page number is still disabled. Here is the code:</p>
  
  <pre><code>   &lt;div style="margin: 10px;"&gt;
          &lt;asp:ListView ID="rptExercise2" runat="server" 
              ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="ExerciseBound" OnPagePropertiesChanging="ChangeExercise"&gt;
              &lt;LayoutTemplate&gt;      
                  &lt;asp:PlaceHolder ID="itemPlaceHolder1" runat="server" /&gt;
              &lt;/LayoutTemplate&gt;
  
              &lt;ItemTemplate&gt;
              &lt;h2&gt;&lt;asp:Label ID="lbName" runat="server" /&gt;&lt;/h2&gt;
              &lt;asp:ListView ID="rptContent2" runat="server" ItemPlaceholderID="itemPlaceHolder2" OnItemDataBound="ContentBound"&gt;
                  &lt;LayoutTemplate&gt;      
                      &lt;asp:PlaceHolder ID="itemPlaceHolder2" runat="server" /&gt;
                  &lt;/LayoutTemplate&gt;
  
                  &lt;ItemTemplate&gt;
                      &lt;div style="padding: 5px;" &gt;                               
                          &lt;div&gt;
                          &lt;asp:Label ID="lbID" runat="server" Text='&lt;%# Eval("ID") %&gt;' Visible="false" /&gt;
  
                          &lt;asp:Label ID="lbWrapper" runat="server" Text='&lt;%# Eval("Wrapper") %&gt;' Visible="false" /&gt;
  
                          &lt;asp:Label ID="lbAudioQuestion" runat="server" Visible="false" /&gt;
  
                          &lt;div style="text-align: right;"&gt;&lt;asp:Label ID="lbAudioAnswer" runat="server" Visible="false" /&gt;&lt;/div&gt;
  
                          &lt;div style="text-align: right;"&gt;&lt;asp:TextBox ID="tbAnswer" runat="server" TextMode="MultiLine" CssClass="tb" Width="50%" Height="60" Visible="false" /&gt;&lt;/div&gt;
  
                          &lt;div style="margin-left: 20px;"&gt;&lt;asp:RadioButtonList ID="rblChoice" runat="server" Visible="false" /&gt;&lt;/div&gt;                               
                          &lt;/div&gt;                                
                      &lt;/div&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:ListView&gt;
  
              &lt;asp:Panel ID="pnFeedback" runat="server" Visible="false" CssClass="Feedback"&gt;
                  &lt;div style="margin: 10px;"&gt;
                      &lt;div style="float: left;"&gt;
                      Score: &lt;br /&gt;&lt;asp:TextBox ID="tbScore" runat="server" CssClass="tb" Width="40" /&gt; / &lt;asp:Label ID="lbScore" runat="server" /&gt;&lt;/div&gt;
                      &lt;/div&gt;
                      &lt;asp:Label ID="lbResultID" runat="server" Visible="false" /&gt;
                      &lt;div style="float: left;"&gt;
                      Feedback&lt;br /&gt;
                      &lt;asp:TextBox ID="tbFeedback" runat="server" TextMode="MultiLine" CssClass="tb" Width="200" Height="60" /&gt;
                      &lt;/div&gt;
  
                      &lt;div style="float: left;"&gt;
                      Recording&lt;br /&gt;
                      &lt;asp:Label ID="lbFeedbackAudio" runat="server" /&gt;
                      &lt;/div&gt;
                      &lt;br style="clear: both;" /&gt;
                  &lt;/div&gt;
              &lt;/asp:Panel&gt;
  
              &lt;/ItemTemplate&gt;
              &lt;EmptyItemTemplate&gt;
                  &lt;div&gt;
                      Sorry, no exercise is found for this lesson.
                  &lt;/div&gt;
              &lt;/EmptyItemTemplate&gt;
  
          &lt;/asp:ListView&gt;
  
  
          &lt;asp:DataPager ID="DataPager" runat="server" PagedControlID="rptExercise2" 
              PageSize="1"&gt;
              &lt;Fields&gt;
                  &lt;asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                      ShowNextPageButton="False" ShowPreviousPageButton="False" /&gt;
                  &lt;asp:NumericPagerField ButtonCount="10" ButtonType="Button" /&gt;
                  &lt;asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                      ShowNextPageButton="False" ShowPreviousPageButton="False" /&gt;
              &lt;/Fields&gt;
          &lt;/asp:DataPager&gt;  
  
  public partial class MyPracClass : System.Web.UI.Page
  {
      // Current page number
      int i = 0;
  
      protected classLessons les;
      protected classExercises exc;
      protected List&lt;classExercises&gt; exercises;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Request["LessonID"] == null)
          {
              return;
          }
  
          classLessons les = new classLessons(Convert.ToInt32(Request["LessonID"]));
          exc = new classExercises();
          exercises = exc.GetExercisesByLessonID(les.ID);
  
          if (!IsPostBack)
          {
              lbLessonName.Text = les.Name;
              lbDescription.Text = les.Description;
              //SetPageNumber();
  
              rptExercise2.DataSource = exercises;
              rptExercise2.DataBind();
          }
      }
  
      protected void ExerciseBound(object sender, ListViewItemEventArgs e)
      {
          if (e.Item.ItemType == ListViewItemType.DataItem)
          {
              ListViewDataItem item = (ListViewDataItem)e.Item;
  
              Label lbName = (Label)item.FindControl("lbName");
              string excName = "Exercise " + (i+1) + ": " + exercises[i].Subject;
              lbName.Text = excName;
  
              Label lbFeedbackAudio = (Label)item.FindControl("lbFeedbackAudio");
              classContent ct = new classContent();
              string test = "exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback";
              lbFeedbackAudio.Text = ct.GetPlayingString("exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback");
  
              ListView rptContent2 = (ListView)item.FindControl("rptContent2");
              Panel pnFeedback = (Panel)item.FindControl("pnFeedback");
  
              if (exercises[i] != null &amp;&amp; rptContent2 != null)
              {
                  classContent cct = new classContent();
  
                  rptContent2.DataSource = cct.GetContentByExerciseID(exercises[i].ID);
                  rptContent2.DataBind();
              }
  
              var result = new classResults();
  
              result.GetResultByExerciseID(Convert.ToInt32(exercises[i].ID));
  
              if (result.ID &gt; 0)
              {
                  pnFeedback.Visible = true;
                  Label lbID = (Label)item.FindControl("lbResultID");
                  Label lbScore = (Label)item.FindControl("lbScore");
                  lbScore.Text = exercises[i].Score.ToString();
                  TextBox tbScore = (TextBox)item.FindControl("tbScore");
                  TextBox tbFeedback = (TextBox)item.FindControl("tbFeedback");
  
                  lbID.Text = result.ID.ToString();
                  tbScore.Text = result.Score.ToString();
                  tbFeedback.Text = result.Feedback;
              }
          }
      }
  
      protected void ChangeExercise(object sender, PagePropertiesChangingEventArgs e)
      {
          // Get current page number
          i = e.StartRowIndex / e.MaximumRows;
  
          rptExercise2.DataSource = exercises;
          rptExercise2.DataBind();
      }
  
      //protected void DataPager_PreRender(object sender, EventArgs e)
      //{
      //    // Get current page number
      //    //i = e.StartRowIndex / e.MaximumRows;
  
      //    rptExercise2.DataSource = exercises;
      //    rptExercise2.DataBind();
      //}
  
      protected void ContentBound(object sender, ListViewItemEventArgs e)
      {
          if (e.Item.ItemType == ListViewItemType.DataItem)
          {
              ListViewDataItem dataItem = (ListViewDataItem)e.Item;
              var qry = (classContent)dataItem.DataItem;
  
              Label lbAudioQuestion = (Label)e.Item.FindControl("lbAudioQuestion");
              Label lbAudioAnswer = (Label)e.Item.FindControl("lbAudioAnswer");
              Label lbWrapper = (Label)e.Item.FindControl("lbWrapper");
              TextBox tbAnswer = (TextBox)e.Item.FindControl("tbAnswer");
              RadioButtonList rblChoice = (RadioButtonList)e.Item.FindControl("rblChoice");
  
              switch (qry.Type)
              {
                  case classContent.ContentType.AudioQuestion:
                      lbAudioQuestion.Visible = true;
                      lbAudioQuestion.Text = qry.GetPlayingString(qry.Wrapper);
                      break;
                  case classContent.ContentType.TextQuestion:
                      lbWrapper.Visible = true;
                      break;
                  case classContent.ContentType.VideoQuestion:
                      lbWrapper.Visible = true;
                      break;
                  case classContent.ContentType.AudioAnswer:
                      lbAudioAnswer.Visible = true;
                      lbAudioAnswer.Text = qry.GetRecordingString("content" + qry.ID + "student" + Session["StudentID"]);
                      break;
                  case classContent.ContentType.TextAnswer:
                      tbAnswer.Visible = true;
                      break;
                  case classContent.ContentType.ChoiceAnswer:
                      rblChoice.DataSource = qry.GetChoiceOptions(qry.ID);
                      rblChoice.DataTextField = "Value";
                      rblChoice.DataValueField = "IsAnswer";
                      rblChoice.DataBind();
                      rblChoice.Visible = true;
                      break;
                  default: break; ;
              }
  
              classAnswers ans = new classAnswers();
  
              var contentAnswer = ans.GetAnswerByContentID(qry.ID);
  
              if (contentAnswer.ID &gt; 0)
              {
                  switch (qry.Type)
                  {
                      case classContent.ContentType.TextAnswer:
                          tbAnswer.Text = contentAnswer.Answer;
                          break;
                      case classContent.ContentType.ChoiceAnswer:
                          rblChoice.SelectedIndex = Convert.ToInt32(contentAnswer.Answer);
                          break;
                      default: break; ;
                  }
              }
  
          }
  
      }
  </code></pre>