11

I have a database driven gridview with paging enabled. All works fine, and is binded as follows on page_load:

sqldataadapter da = new saldatadapter("sql query"), con);
datatable dt = new datatable();
gridview1.datasource = dt;
gridview1.databind();

Is there an option which I can enable to the page number automatically appears in the url? The reason I want to do this is so I can email the url with the page number, then when the user clicks the url, it causes the gridview to display data from the correct page.

UPDATE 2 - Current full code as requested:

public partial class conflict_search_Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Request.QueryString["page"] != null)
            {

                int index = int.Parse(Request.QueryString["page"]);
                GridView1.PageIndex = index;
                BindData();


            }
            else
            {

                BindData();

            }

        }
        else
        {

            BindData();

        }
    }

    private void BindData()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connString"]);
        SqlDataAdapter da = new SqlDataAdapter("sql query here which returns over 100 pages", con);

        DataTable dt = new DataTable();
        da.Fill(dt);

        GridView1.DataSource = dt;

        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int index = e.NewPageIndex + 1;
        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        e.Cancel = true;

        Response.Redirect(string.Format("{0}?page={1}", url, index));
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}

This gives me an error when I try clicking on the paging numbers at the bottom of the datagrid. The error is as follows:

If I load the page fresh, it will load. If I then click on page number 5, it displays ?page=5 in the url which is what I expect, but for some reason, page 6 is selected on the pagination numbers at the bottom of the screen. If I then click page 10 for example, the url changes to ?page=5?page=10 which is clearly wrong, which gives the error:

Input string was not in a correct format. 
int index = int.Parse(Request.QueryString["page"]);
0

5 Answers 5

7

Use:

protected void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
    int index = e.NewPageIndex + 1;
    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    e.Cancel;

    Response.Redirect(string.Format("{0}?page={1}", url, index));
}

PageLoad(...)
{
    if (!Page.IsPostBack)
    {
          if (Request.QueryString["page"] != null)
          {
              int index = int.Parse(Request.QueryString["page"]);
              // bind your gridview
             GridView1.PageIndex = index;
          }
    }
}

from GridView Paging and Sorting with url parameters

Sign up to request clarification or add additional context in comments.

8 Comments

e.Cancel gives me the error CS0201: Only assignment, call, increment, decrements, and new object expressions can be used as statement.
@oshirowanen e.Cancel = true;
@oshirowanen but not e.Cancel() = true; no ()
Thanks, that error has been fixed, but now when I type in ?page=7 at the end of the url, it still goes to page 1. If I click on page 7 from the page numbers at the bottom of the gridview, I get the following error: int index = int.Parse(Request.QueryString["page"]); System.FormatException. Input string was not in correct format.
@oshirowanen - please post your complete relevant code - include part updated with the code from my answer that you used
|
3
+200

If you change the code in function GridView1_PageIndexChanging as below everything would work:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   e.Cancel = true;
   int index = e.NewPageIndex;
   string urlPath =  HttpContext.Current.Request.Url.AbsoluteUri;
   Uri uri = new Uri(urlPath);
   string url = uri.GetLeftPart(UriPartial.Path);
   Response.Redirect(string.Format("{0}?page={1}", url, index));
}

Also you dont need to handel GridView1_PageIndexChanged event.

Comments

3

Try this , set PageIndex before BindData()

if (!Page.IsPostBack)
        {

            if (Request.QueryString["page"] != null)
            {

                int index = int.Parse(Request.QueryString["page"]);
                GridView1.PageIndex = index;
                BindData();


            }
            else
            {

                BindData();

            }

        }

Comments

2

When you change the Page index for the gridview, you have to Rebind it, or set the page index before binding:

        if (!string.IsNullOrEmpty(Request.QueryString["page"]) && int.Parse(Request.QueryString["page"]) < GridView1.PageCount)
        {
            GridView1.PageIndex = int.Parse(Request.QueryString["page"]);
            GridView1.DataBind();
        }

Comments

2

Why are you calling BindData() on GridView1_PageIndexChanged

and when debugging the exception, what exactly do you get as a value for Request.QueryString["page"]

Maybe I'm wrong, but does it have any value at all?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.