Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,
My current project is a real estate website. In this I have to display the details of the properties in small boxes. I did it successfully using data list in rows and columns. But I want to enable paging in data list. I searched in Google and got many codes. But none of that satisfied my requirement.

Here is my .aspx code
C#
<table>
<tr>
    <td>
<asp:DataList ID="DataList1" runat="server" DataKeyField="fldId" 
        DataSourceID="SqlDataSource1" RepeatColumns="2" OnSelectedIndexChanged="dlProducts_SelectedIndexChanged">
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" Height="111px" 
                ImageUrl='<%# Eval("fldImage", "~/Property/{0}") %>' Width="247px" />           
.
.
.


        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [fldId], [fldImage], [fldCategory] FROM [tblMaster]">
        <SelectParameters>
            <asp:Parameter DefaultValue="1" Name="fldAd" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
</td></tr?
<tr>
            <td>
                <asp:Label ID="lblCurrentPage" runat="server" Visible="true"></asp:Label>
            </td>
        </tr>

        <tr>
            <td align="center">
                <asp:Button ID="cmdPrev" runat="server" Text=" << " OnClick="cmdPrev_Click" />
                <asp:Button ID="cmdNext" runat="server" Text=" >> " OnClick="cmdNext_Click" />

            </td>
        </tr>
        </table>



I tried this .aspx.cs code, but got the error
"
C#
The name 'CurrentPage' does not exist in the current context.
"

.aspx.cs
C#
protected void items()
  {
      PagedDataSource objDs = new PagedDataSource();
      DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
      objDs.DataSource = dv;
      objDs.AllowPaging = true;
      objDs.PageSize = 5;
      objDs.CurrentPageIndex = CurrentPage;
      lblCurrentPage.Text = "Page:" + (CurrentPage + 1).ToString() + " Of " + objDs.PageCount.ToString();
      cmdPrev.Enabled = !objDs.IsFirstPage;
      cmdNext.Enabled = !objDs.IsLastPage;
      DataList1.DataSource = objDs;
      DataList1.DataBind();

  }


  protected void cmdPrev_Click(object sender, EventArgs e)
  {
      try
      {
          CurrentPage -= 1;
          items();
      }
      catch (Exception ex)
      {
          Logger.LogException(ex);
      }
  }

  protected void cmdNext_Click(object sender, EventArgs e)
  {
      try
      {

          CurrentPage += 1;
          items();
      }
      catch (Exception ex)
      {
          Logger.LogException(ex);
      }
  }


How can I remove this error or is there any other methods to apply paging in Datalist? Please give me any ideas to overcome my problem.
Thank you...
Posted
Updated 25-Jul-12 20:29pm
v6
Comments
Sandeep Mewara 26-Jul-12 2:22am    
You use currentpage everywhere. Where is that defined?

Is this your code? Looks like a copy-paste.. from where?

Well, code shared across does not have CurrentPage declaration at all. In order to drive data using pagination, you are using variable 'Currentpage' all through which looks missing. Hence the error.

Define the variable, set it at load for first time. Try out.
 
Share this answer
 
Mohamed Mitwalli thanking you for this post
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900