【问题标题】:autochange page index in gridview asp.net在gridview asp.net中自动更改页面索引
【发布时间】:2017-07-13 08:59:38
【问题描述】:

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

【问题讨论】:

    标签: asp.net gridview page-index-changed


    【解决方案1】:

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

    PageIndex可以是0PageCount可以是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(); 
    }
    

    【讨论】:

    • 谢谢你,非常感谢。这个对我有用。再次感谢。
    【解决方案2】:

    您首先需要确保处理了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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多