asp.netgridviewpage-index-changed

autochange page index in gridview asp.net


i have one wallboard application in which i have to display some record in a gridview. as number of records are large in numbers so i have to implement paging in gridview. but as it is wallboard application user cannot change the page. so after each 10 seconds i have to show the next page.

CS file

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

above is the code which i tried with ticker.

if i try to use GV_ExtCallSummary.PageIndex++ nothing happens. just increase in pageindex.

and if i use setpageindex(1) it throws exception that is:

The GridView 'GV_ExtCallSummary' fired event PageIndexChanging which wasn't handled.

even thought the function do exist.

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

and this function works fine if i click on page numbers.

HTML if any one wants to see html code

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


Solution

  • .Aspx Code:

    Put your GridView inside ContentTemplate of the UpdatePanel.

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

    PageIndex can be 0 and PageCount can be is 1, so that they can't be equal. Therefore, you have to use this code:

    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(); 
    }