Navigating gridview pages from url values
Asked Answered
K

5

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"]);
Kurbash answered 4/10, 2013 at 14:55 Comment(0)
O
3

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.

Oxygen answered 12/2, 2014 at 9:10 Comment(0)
C
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

Chromatic answered 8/10, 2013 at 6:28 Comment(8)
e.Cancel gives me the error CS0201: Only assignment, call, increment, decrements, and new object expressions can be used as statement.Kurbash
@Kurbash e.Cancel = true;Chromatic
@Kurbash but not e.Cancel() = true; no ()Chromatic
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.Kurbash
@Kurbash - please post your complete relevant code - include part updated with the code from my answer that you usedChromatic
@Kurbash - post it not as a comment :) Update your first postChromatic
HttpContext.Current.Request.Url.AbsoluteUri contain query parameters. Remove ? and every characters after it.Shock
@oshiorowanen - When you give the url alone with page number paging wont happen because the code is inside !PageIsPostBack. Hence I would recommend you to put the code outside of the if condition and only check if the querystring has the pagenumber value!Implausible
J
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();

            }

        }
Jag answered 6/2, 2014 at 21:3 Comment(0)
O
3

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.

Oxygen answered 12/2, 2014 at 9:10 Comment(0)
V
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();
        }
Vista answered 10/10, 2013 at 5:9 Comment(0)
S
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?

Sod answered 6/2, 2014 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.