在 GridView asp.net 中自动更改页面索引



我有一个墙板应用程序,我必须在网格视图中显示一些记录。 由于记录数很大,所以我必须在 GridView 中实现分页。 但由于它是墙板应用程序,用户无法更改页面。 所以每 10 秒后我必须显示下一页。

CS 文件

protected void Timer1_Tick1(object sender, EventArgs e)
{
if (GV_ExtCallSummary.PageCount == GV_ExtCallSummary.PageIndex)
{
//    timer1.Enabled = false;
//    GV_ExtCallSummary.PageIndex = 1;
}
else
{
try
{
//  GV_ExtCallSummary.PageIndex++;
GV_ExtCallSummary.SetPageIndex(1);
//  GV_ExtCallSummary.DataSource = dt;
GV_ExtCallSummary.DataBind(); 
}
catch(Exception ex)
{
string exv = ex.Message;
}
}
}

以上是我用股票代码尝试的代码。

如果我尝试使用GV_ExtCallSummary.PageIndex++什么也没发生。 只是增加pageindex.

如果我使用setpageindex(1)它会抛出异常,即:

GridView "GV_ExtCallSummary"触发了未处理的事件 PageIndexChange。

甚至认为该功能确实存在。

protected void GV_ExtCallSummary_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV_ExtCallSummary.PageIndex = e.NewPageIndex;
GV_ExtCallSummary.DataSource = dt;
GV_ExtCallSummary.DataBind();
}

如果我单击页码,此功能可以正常工作。

HTML如果有人想查看 html 代码

<asp:GridView ID="GV_ExtCallSummary" runat="server" AutoGenerateColumns="false" 
Width="100%" Visible="true" OnRowDataBound="GV_ExtCallSummary_RowDataBound"  OnPageIndexChanging="GV_ExtCallSummary_PageIndexChanging"
EmptyDataText="No data exist." AllowPaging="True" CssClass="table" HeaderStyle-BackColor="#669999" 
AlternatingRowStyle-CssClass="success" PageSize="10">
<Columns> 
<asp:TemplateField HeaderText="Extention">
<ItemTemplate>
<asp:Label ID="lblExt" runat="server" Text='<%# Bind("Extension") %>' />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblExtName" runat="server" Text='<%# Bind("ExtnName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Abandon">
<ItemTemplate>
<asp:Label ID="lblAdandon" runat="server" Text='<%# Bind("Abandon") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Incoming">
<ItemTemplate>
<asp:Label ID="lblIncoming" runat="server" Text='<%# Bind("Incoming") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Outgoing">
<ItemTemplate>
<asp:Label ID="lblOutgoing" runat="server" Text='<%# Bind("Outgoing") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Intercom">
<ItemTemplate >
<asp:Label ID="lbl_Intercom" runat="server" Text='<%# Bind("Intercom") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#8AC5FF" Font-Bold="True" ForeColor="White" />

.Aspx 代码:

把你的GridView放在UpdatePanelContentTemplate里。

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
<Triggers>
<asp:AsyncPostBackTrigger  ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate> 
<!-- Here will be your Gridview Code -->
</ContentTemplate> 
</asp:UpdatePanel> 
<!-- Timer will tick after 10 seconds -->
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>

.CS代码:

PageIndex可以是0的,PageCount可以是1的,所以它们不能相等。因此,您必须使用此代码:

protect void Timer1_Tick(object sender, EventArgs e)
{
if(GV_ExtCallSummary.PageIndex == (GV_ExtCallSummary.PageCount -1))
{
GV_ExtCallSummary.PageIndex = 0;
}
else
{
GV_ExtCallSummary.PageIndex = GV_ExtCallSummary.PageIndex + 1;
}
GV_ExtCallSummary.DataSource = dt; // e.g. dt can be your be your data to set
GV_ExtCallSummary.DataBind(); 
}

首先需要确保处理PageIndexChanging事件。

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int pageIndex = GridView1.PageIndex + 1;
//if you want the autmatic page change to stop at the end
if (pageIndex == GridView1.PageCount)
{
Timer1.Enabled = false;
return;
}
//or loop continuously by going back to the first page
if (pageIndex == GridView1.PageCount)
{
pageIndex = 0;
}
//set the new pageindex and rebind the gridview
GridView1.PageIndex = pageIndex;
GridView1.DataSource = mySource;
GridView1.DataBind();
}

并在Tick中调用该方法。

protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1_PageIndexChanging(null, null);
}

最新更新