user2534035
user2534035

Reputation:

Going to a specific page number in a GridView paging feature

I am trying to implement GO TO page by allowing the user to enter page number in a text box and clicking a button texted as GO.

.aspx code is;

<asp:Label ID="lblGoToPage" runat="server" Text="Go To Page : "></asp:Label>
<asp:TextBox ID="txtGoToPage" runat="server" Width="47px"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />

and .cs code is;

protected void btnGo_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt16(txtGoToPage.Text);
        txtGoToPage.Text = "";
    }

These above lines of code are giving out but not the desired one. Cant figure out where I am going wrong. Any help will be much appreciated. Thanks in advance.

Upvotes: 1

Views: 6562

Answers (1)

dcp
dcp

Reputation: 55434

You need to rebind the grid after you change the page index.

protected void btnGo_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt16(txtGoToPage.Text) -1; //since PageIndex starts from 0 by default.
        txtGoToPage.Text = "";
        GridView1.DataBind()
    }

Upvotes: 2

Related Questions