Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating gridview pages from url values

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"]);
like image 458
oshirowanen Avatar asked Oct 04 '13 14:10

oshirowanen


4 Answers

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.

like image 197
Shashank Chaturvedi Avatar answered Nov 02 '22 22:11

Shashank Chaturvedi


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

            }

        }
like image 35
akbar ali Avatar answered Nov 03 '22 00:11

akbar ali


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

like image 7
MikroDel Avatar answered Nov 02 '22 22:11

MikroDel


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();
        }
like image 2
John DeLeon Avatar answered Nov 03 '22 00:11

John DeLeon