GridView.PageSize 在第一页加载时设置为默认 10
我正在使用 C#、.NET 3.5。我有一个带有动态绑定数据源的 GridView 控件,并且在 Page_Load 事件处理程序中动态设置 PageSize。我已经在控件上设置了AllowPaging = true。 GridView分页工作正常,但是Page_Load中设置的页面大小在第一次加载页面时不会生效。第一次加载页面时,它将始终显示 10 行,无论我设置的 GridView.PageSize 属性如何(5、15 等)。第一次(页面回发)后,页面大小生效,一切按预期工作。
页面大小是我从 appsettings 下的 web.Config 文件中获取的母版页的属性。
我不知道为什么 gridView 的页面大小第一次不生效。我应该在 Page_Load 之外的另一个事件中设置页面大小吗?另外,我总是设置它,即使它是回发。我正在使用内部网络服务器运行该页面。知道发生了什么吗?
隐藏代码(在aspx页面上GridView1.AllowPaging = true):
protected void Page_Load(object sender, System.EventArgs e)
{
DataView dvMembers = GetMembers;
GridView1.DataSource = dvMembers;
GridView1.PageSize = Master.GridViewSize;
}
母版页属性:
public int GridViewSize
{
get { return Convert.ToInt32
(ConfigurationManager.AppSettings ["memberDataGridPageSize"]); }
}
I am using C#, .NET 3.5. I have a GridView control with dynamically bound DataSource and I set the PageSize dynamically in the Page_Load event handler. I have set AllowPaging = true on the control.
The GridView paging is working fine, however the pagesize set in Page_Load does not take effect the first time that the page is loaded. The first time that the page is loaded, it will always display 10 rows irrespective of the GridView.PageSize property that I have set (5, 15 etc). After the 1st time (page postback), the page size takes effect and everything works as expected.
The Page size is a property of the Master Page that I get from the web.Config file under appsettings.
I am not sure why the pagesize of the gridView does not take effect the 1st time. Should I be setting the pagesize in another event other than the Page_Load. Also, I am setting it always, even if its a postback. I am running the page using the internal web server. Any idea whats happening?
code behind (GridView1.AllowPaging = true on aspx page):
protected void Page_Load(object sender, System.EventArgs e)
{
DataView dvMembers = GetMembers;
GridView1.DataSource = dvMembers;
GridView1.PageSize = Master.GridViewSize;
}
Master page property :
public int GridViewSize
{
get { return Convert.ToInt32
(ConfigurationManager.AppSettings ["memberDataGridPageSize"]); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子页面的 PageLoad 事件在母版页的 PageLoad 事件之前调用。因此,如果您在母版页PageLoad 事件中设置
Master.GridViewSize
,则直到第二次PostBack 时才会设置它。最好在页面生命周期的早期加载 GridViewSize,然后将其存储在会话中。
The PageLoad event of your child page is called before the PageLoad event of your master page. Therefore, if you are setting
Master.GridViewSize
in the master page PageLoad event, it is not set until the second PostBack.It would be better to load the GridViewSize early in the page lifecycle and then store it in the session.
在 GridView 的分页定义中,它是在第一个数据绑定时设置的,但如果发生回发,则不会在第二个数据绑定中设置。(默认情况下没有分页)
您需要在aspx页面上为GridView设置分页。
尝试这样
还知道,如果您正在运行 ASP.NET 3.5 项目,您可以使用
DataPager
控件扩展 GridView,有一个 Matt Berseth 开发的示例 此处。In your paging definition for GridView is set on first databinding but not in second if a postback occurs.(By default there is no paging)
You need to set paging for GridView on aspx page.
Try like this
Also know that If you are running ASP.NET 3.5 project,you can use
DataPager
control with extending GridView,there is an example which Matt Berseth developed here.设置页面大小后缺少 PageBind 方法。添加该内容,一切正常。
我在设置 PageSize 之前绑定了控件,我猜想该值在第一次之后被保存,并在随后的回发中使用,但不是第一次。
感谢贾森·伯坎指出了这一点。
Was missing the PageBind method after setting the Pagesize. Adding that and its all working fine.
I was binding the control before setting the PageSize and I guess that value was being saved after the 1st time and being used on Postback subsequently but not the first time.
Thanks Jason Berkan for pointing that out.