SharePoin2010 的"SPGridView "控件常见的两个问题

问题一:无法显示分页的页码。

很多朋友很奇怪明明已经设置如下的代码

oGrid.AllowPaging = true; 
oGrid.PageSize = 2; 
oGrid.PageIndexChanging +=new GridViewPageEventHandler(oGrid_PageIndexChanging);

但是页面上无法显示页码。

这个是SharePoint 自己的问题,我们需要加入下面一行代码

Controls.Add(oGrid); 
oGrid.PagerTemplate = null;//这行就是要加的代码,顺序很重要,一定要在Controls.Add之后,DataBind();之前 
oGrid.DataBind();

现在再试试,是不是就可以看见页码了^_^

  

问题二:启用了过滤后,在翻页以后,发现在选择下一页,SPGridView会加载所有的数据,而不是filter过滤的数据。过滤功能失效了。

因为在render 下一页后,无法继续保存filter .所以这里有这样一个办法大家可以试试:

首先在OnPreRender里插入下面代码保存FilterExpression 到ViewState中,(记得确保代码里Enable ViewState)

protected override void OnPreRender(EventArgs e) {
    ViewState["FilterExpression"] = odsDataSource.FilterExpression;
    base.OnPreRender(e);
}

然后

在您的Controls.Add(odsDataSource); 之前插入下面的代码

HttpRequest req = HttpContext.Current.Request; 
if (req.Form["__CALLBACKID"] == null ||
    req.Form["__CALLBACKPARAM"] == null ||
    !req.Form["__CALLBACKID"].EndsWith("ExampleGrid"))
{
    if (ViewState["FilterExpression"] != null)
        odsDataSource.FilterExpression = (string)ViewState["FilterExpression"];
}
原文地址:https://www.cnblogs.com/masahiro/p/10128988.html