views:

1387

answers:

4

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

+1  A: 

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.

Jason Berkan
The Master.GridViewSize is a property of the master page and is not set in the Page_Load event. And when I debug the content page to see its value in the Page_Load event handler, it is already set. So I dont think that is the problem.What event do you think I should set the grid page size in?
Pritika
If the value is set the first time through, then that is not the problem. What happens if you hardcode a PageSize?
Jason Berkan
Also, where is your call to GridView1.DataBind() or Page.DataBind()?
Jason Berkan
Hi Jason, The GridViewBind is happening inside the GetMembers method, before setting the pageSize based on some logic. I am now calling Page.DataBind () after setting the Pagesize and its working. Thanks for your help.
Pritika
A: 

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.

Myra
I am setting the pagesize and the datasource in the Page_load event handler irrespective of whether it is a postback or not. So I am not sure why there is a different between the 1st time and then postbacks.Thanks for your suggestion to insert the pagesize in the ASPX code itself but I would like to find out whats happening with my current code when I set the pagesize in the code behind.
Pritika
A: 

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.

Pritika
Feel free to accept this answer, so that later people searching on this question can easily find the answer.
Jason Berkan
You need to accept an answer if you have a solution for it.
Myra
A: 

Thanks Jason Berkan.you help me lot.

Randheer