在Asp.net中的数据列表中分页 [英] Paging in Datalist in Asp.net

查看:58
本文介绍了在Asp.net中的数据列表中分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我当前的项目是房地产网站.在此,我必须在小框中显示属性的详细信息.我使用行和列中的数据列表成功完成了此操作.但是我想在数据列表中启用分页.我在Google中搜索了许多代码.但这都不满足我的要求.

这是我的.aspx代码

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

<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>




我尝试了此.aspx.cs代码,但收到错误
"




I tried this .aspx.cs code, but got the error
"

The name 'CurrentPage' does not exist in the current context.

"

.aspx.cs

"

.aspx.cs

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...

推荐答案

ConnectionStrings:ConnectionString%>" SelectCommand = " > < SelectParameters> < asp:参数DefaultValue = " Name = " Type = " /> </ SelectParameters > </ asp:SqlDataSource > </ td > </ tr? < tr > < td> < asp:标签ID = " runat = " Visible = " > </ asp:Label > </ td > </ tr > < tr> < td align = " >> < asp:按钮ID = " runat = " Text = " OnClick = cmdPrev_Click"/> < asp:按钮ID = " runat = " Text = " OnClick = cmdNext_Click"/> </ td > </ tr > </ >
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>




我尝试了此.aspx.cs代码,但收到错误
"




I tried this .aspx.cs code, but got the error
"

The name 'CurrentPage' does not exist in the current context.

"

.aspx.cs

"

.aspx.cs

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...



检查此
http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx [ ^ ]
http://aspalliance.com/157_Paging_in_DataList [ ^ ]
http://forums.asp.net/t/1108198.aspx [
Hi ,
Check this
http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx[^]
http://aspalliance.com/157_Paging_in_DataList[^]
http://forums.asp.net/t/1108198.aspx[^]
Best Regards
M.Mitwalli


好吧,共享的代码根本没有CurrentPage声明.为了使用分页驱动数据,您正在使用变量"Currentpage",看起来始终缺少变量.因此,错误.

定义变量,首次将其设置为加载状态.试试吧.
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.


这篇关于在Asp.net中的数据列表中分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆