1

我有一个墙板应用程序,我必须在网格视图中显示一些记录。由于记录数量很大,所以我必须在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;
        }
    }
}

以上是我用ticker尝试的代码。

如果我尝试使用GV_ExtCallSummary.PageIndex++没有任何反应。只是增加pageindex

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

GridView 'GV_ExtCallSummary' 触发了未处理的事件 PageIndexChanging。

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

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

4

2 回答 2

0

您首先需要确保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);
}
于 2017-07-13T10:15:19.230 回答
0

.aspx 代码:

把你的GridView里面。ContentTemplateUpdatePanel

<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 代码:

PageIndexcan be0PageCountcan be is 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(); 
}
于 2017-07-13T15:36:47.603 回答