GridView.PageSize 在第一页加载时设置为默认 10

发布于 2024-08-08 06:43:56 字数 951 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-08-15 06:43:56

子页面的 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.

╄→承喏 2024-08-15 06:43:56

在 GridView 的分页定义中,它是在第一个数据绑定时设置的,但如果发生回发,则不会在第二个数据绑定中设置。(默认情况下没有分页)
您需要在aspx页面上为GridView设置分页。

尝试这样

<asp:GridView ID="GridView1" PageSize='<%$ AppSettings:memberDataGridPageSize %>' ..>

还知道,如果您正在运行 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

<asp:GridView ID="GridView1" PageSize='<%$ AppSettings:memberDataGridPageSize %>' ..>

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.

剪不断理还乱 2024-08-15 06:43:56

设置页面大小后缺少 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文