gpt4 book ai didi

asp.net - DataList 中的分页不起作用

转载 作者:行者123 更新时间:2023-12-02 21:21:38 27 4
gpt4 key购买 nike

我的页面上有 DataList,用于显示来自数据库的记录。我已关注this使用 DataList 实现分页的教程。

它在第一页上显示 5 条记录,但在下一页上,数据列表加载为空。我可以通过单击“下一步”和“上一步”按钮看到当前页面更改的值。

以下是我的aspx页面代码:

<asp:DataList ID="DataList1" runat="server">
<HeaderTemplate>
<table class="TicketTableInner">
<tr>
<td class="ticketlistheaderrow" align="left" valign="middle" width="8%">Ticket ID</td>
<td class="ticketlistheaderrow" align="left" valign="middle" width="21%">Category</td>
<td class="ticketlistheaderrow" align="left" valign="middle" width="41%">Problem</td>
<td class="ticketlistheaderrow" align="left" valign="middle" width="15%">Create Date</td>
<td class="ticketlistheaderrow" align="left" valign="middle" width="15%">Reply Date</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="TicketRow<%#Eval("IsReadString")%>" onclick="UpdateTicketID('<%#Eval("TicketIDD")%>')">
<td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#Eval("TicketIDFROMDB")%></td>
<td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#TrimLenght(Eval("Category"), 30)%></td>
<td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#TrimLenght(Eval("Problem"), 70)%></td>
<td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%#Eval("CreatedDate")%></td>
<td class="ticketlistRowItem<%#Eval("IsReadString")%>"><%# ProcessMyDataItemForNA(Eval("RepliedDate"))%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<table class="style1">
<tr>
<td class="style2">
<asp:LinkButton ID="LinkButton2" runat="server"
OnClick="LinkButton2_Click">Next</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server"
OnClick="LinkButton3_Click">Previous</asp:LinkButton>
</td>
</tr>
</table>

以下是代码隐藏中的代码:

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class supportTickets_myTickets
Inherits System.Web.UI.Page
Public TicketStatus As String = ""
Public TicketID As String = ""
Dim pg As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
CurrentPageIndex = 0
showData()
Else
End If
End Sub

Private Function GetDataTable() As DataTable
'//This function returns record from database in the form of dataTable
End Function


Private Sub showData()
Dim pgd As New PagedDataSource()
Dim ds As DataSet = GetDataSet()
pgd.DataSource = ds.Tables(0).DefaultView
pgd.AllowPaging = True
pgd.PageSize = 5
pgd.CurrentPageIndex = CurrentPageIndex

LinkButton2.Enabled = Not (pgd.IsLastPage)
LinkButton3.Enabled = Not (pgd.IsFirstPage)

DataList1.DataSource = pgd
DataList1.DataBind()
End Sub


Public Property CurrentPageIndex() As Integer
Get
If ViewState("pg") Is Nothing Then
Return 0
Else
Return Convert.ToInt16(ViewState("pg"))
End If
End Get
Set(value As Integer)
ViewState("pg") = value
End Set
End Property


Protected Sub LinkButton2_Click(sender As Object, e As EventArgs)
CurrentPageIndex += 1
showData()
End Sub
Protected Sub LinkButton3_Click(sender As Object, e As EventArgs)
CurrentPageIndex -= 1
showData()
End Sub

End Class

最佳答案

通过分页数据源绑定(bind)DataList

 public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}



private void BindListing()
{
DataTable dt = Method which return datatable;
if (dt.Rows.Count <= 0)
{
lnknext.Visible = false;
lnkPrevious.Visible = false;
ScriptManager.RegisterStartupScript(this, this.GetType(), "Error", "alert('No records found!!!.');", true);
return;
}
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dt);
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = 10;
pgitems.CurrentPageIndex = PageNumber;

DataList1.DataSource = pgitems;
DataList1.DataBind();

}



protected void lnkPrevious_Click(object sender, EventArgs e)
{
PageNumber -= 1;
BindListing();
}
protected void lnknext_Click(object sender, EventArgs e)
{

PageNumber += 1;
BindListing();
}



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindListing();
}

}

我希望上面的例子能对你有所帮助..

关于asp.net - DataList 中的分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com