【问题标题】:Navigating gridview pages from url values从 url 值导航 gridview 页面
【发布时间】:2013-10-11 15:21:03
【问题描述】:

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

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

是否有一个选项可以让页码自动出现在 url 中?我想这样做的原因是我可以通过电子邮件发送带有页码的 url,然后当用户单击 url 时,它会导致 gridview 显示来自正确页面的数据。

更新 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 页,它会在 url 中显示 ?page=5,这是我所期望的,但由于某种原因,在屏幕底部的分页号上选择了第 6 页。例如,如果我然后单击第 10 页,则 url 更改为 ?page=5?page=10 这显然是错误的,这会给出错误:

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

【问题讨论】:

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


    【解决方案1】:

    用途:

    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

    【讨论】:

    • e.Cancel 给了我错误CS0201: Only assignment, call, increment, decrements, and new object expressions can be used as statement
    • @oshirowanen e.Cancel = true;
    • @oshirowanen 但不是 e.Cancel() = true;没有()
    • 谢谢,该错误已修复,但现在当我在网址末尾输入?page=7 时,它仍会转到第 1 页。如果我从页码中单击第 7 页在gridview的底部,我收到以下错误:int index = int.Parse(Request.QueryString["page"]); System.FormatException. Input string was not in correct format.
    • @oshirowanen - 请发布您完整的相关代码 - 包括使用您使用的我的答案中的代码更新的部分
    【解决方案2】:

    如果您将函数 GridView1_PageIndexChanging 中的代码更改如下,一切都会正常工作:

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

    你也不需要处理GridView1_PageIndexChanged事件。

    【讨论】:

      【解决方案3】:

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

      if (!Page.IsPostBack)
              {
      
                  if (Request.QueryString["page"] != null)
                  {
      
                      int index = int.Parse(Request.QueryString["page"]);
                      GridView1.PageIndex = index;
                      BindData();
      
      
                  }
                  else
                  {
      
                      BindData();
      
                  }
      
              }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          你为什么在GridView1_PageIndexChanged上打电话给BindData()

          在调试异常时,您究竟会得到什么作为Request.QueryString["page"] 的值

          也许我错了,但它有任何价值吗?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-02
            相关资源
            最近更新 更多