从url值导航gridview页面

时间:2013-10-04 14:55:22

标签: c# asp.net .net gridview .net-3.5

我有一个启用了分页的数据库驱动的gridview。一切正常,并在page_load上绑定如下:

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

是否有可以启用页面编号自动显示在网址中的选项?我想这样做的原因是我可以通过电子邮件发送带有页码的网址,然后当用户点击网址时,它会使网格视图显示来自正确页面的数据。

更新2 - 请求的当前完整代码:

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

当我尝试点击数据网格底部的分页号时,这会给我一个错误。错误如下:

如果我将页面加载为新的,则会加载。如果我然后单击第5页,它会在网址中显示?page=5,这是我所期望的,但出于某种原因,在屏幕底部的分页编号上选择了第6页。如果我然后点击第10页,例如,网址会更改为?page=5?page=10,这显然是错误的,这会产生错误:

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

5 个答案:

答案 0 :(得分:7)

使用:

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

来自GridView Paging and Sorting with url parameters

答案 1 :(得分:3)

试试这个,在BindData()之前设置PageIndex

if (!Page.IsPostBack)
        {

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

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


            }
            else
            {

                BindData();

            }

        }

答案 2 :(得分:3)

如果您更改GridView1_PageIndexChanging函数中的代码,如下所示: - / p>

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

你也不需要handel GridView1_PageIndexChanged事件。

答案 3 :(得分:2)

当你更改gridview的Page索引时,你必须重新绑定它,或者在绑定之前设置页面索引:

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

答案 4 :(得分:2)

您为何在BindData()

上致电GridView1_PageIndexChanged

并且在调试异常时,您到底得到的是Request.QueryString["page"]

的值

也许我错了,但它有什么价值吗?