This is a migrated thread and some comments may be shown as answers.

RadGrid doesn't maintain custom page sizes

6 Answers 325 Views
Grid
This is a migrated thread and some comments may be shown as answers.
golddog
Top achievements
Rank 1
golddog asked on 03 Feb 2012, 10:43 PM
Added custom sizes 100 and 200 to the combobox in item created as shown.  Our grid does an ajax postback to query for more data if page size or number changes.
protected void partnerGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridPagerItem)
    {
        RadComboBox pageSize = (RadComboBox)e.Item.FindControl("PageSizeComboBox");
        RadComboBoxItem rcbItem100 = pageSize.FindItemByText("100");
        RadComboBoxItem rcbItem200 = pageSize.FindItemByText("200");
        if (rcbItem100 == null)
        {
            pageSize.Items.Add(new RadComboBoxItem("100"));
            pageSize.FindItemByText("100").Attributes.Add("ownerTableViewId", partnerGrid.MasterTableView.ClientID);
        }
 
        if (rcbItem200 == null)
        {
            pageSize.Items.Add(new RadComboBoxItem("200"));
            pageSize.FindItemByText("200").Attributes.Add("ownerTableViewId", partnerGrid.MasterTableView.ClientID);
        }
 
        pageSize.SelectedValue = partnerGrid.PageSize.ToString();
    }
}


Inexplicably, ItemCreated is fired four times when the pagesize is changed. I suppose two are for each pager (even though
it's set to top-only, the bottom pager is still there), but I can't fathom the last two.

Worse, on the 3rd and 4th invocations of ItemCreated, the combobox has remembered the standard three (10, 20, 50) plus whatever
was selected, but has lost the unselected one. So, if the user goes directly to 200, when the grid re-renders
it shows 100 below 200 (as it was added but 200 was already there).

Secondly, on subsequent postbacks, the combobox will not accept a selectedValue of whatever the fifth item is now
and keeps defaulting to 10. The grid's page size is fine, so the query gets the appropriate number of items,
we just show 10 in the pulldown instead of the number they actually chose.

The ajax is really simple:

<telerik:AjaxSetting AjaxControlID="partnerGrid">
    <UpdatedControls>
        <telerik:AjaxUpdatedControl ControlID="partnerGrid" LoadingPanelID="gridLoadingPanel" />
    </UpdatedControls>
</telerik:AjaxSetting>

Anytime we do something in the grid, show the loading panel while we're thinking about it. It seems as if
something's wrong in ajax land, but it can't be simpler than that.

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Feb 2012, 07:07 AM
Hello,

I am not able to reproduce the issue please check below code snippet.
  <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
 
 
 
 
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
            ShowFooter="true"  AutoGenerateColumns="false"
     AllowPaging="true"
   
OnItemCreated="RadGrid1_ItemCreated">
            <PagerStyle AlwaysVisible="true" />
            <MasterTableView  PageSize="2">
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID">
                    </telerik:GridBoundColumn>
                   
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
   {
 
       dynamic data = new[] {
               new { ID = "1", Name ="Name11",ParentID = "0"},
               new { ID = "2", Name ="Name11",ParentID = "1"},
               new { ID = "3", Name ="Name11",ParentID = "2"},
               new { ID = "4", Name ="Name1",ParentID = "3"}
           };
 
       RadGrid1.DataSource = data;
 
   }
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridPagerItem)
       {
           RadComboBox pageSize = (RadComboBox)e.Item.FindControl("PageSizeComboBox");
           RadComboBoxItem rcbItem100 = pageSize.FindItemByText("100");
           RadComboBoxItem rcbItem200 = pageSize.FindItemByText("200");
           if (rcbItem100 == null)
           {
               pageSize.Items.Add(new RadComboBoxItem("100"));
               pageSize.FindItemByText("100").Attributes.Add("ownerTableViewId", RadGrid1.MasterTableView.ClientID);
           }
 
           if (rcbItem200 == null)
           {
               pageSize.Items.Add(new RadComboBoxItem("200"));
               pageSize.FindItemByText("200").Attributes.Add("ownerTableViewId", RadGrid1.MasterTableView.ClientID);
           }
 
           pageSize.SelectedValue = RadGrid1.PageSize.ToString();
       }
   }


Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Feb 2012, 07:11 AM
Hello,

If you get issue then using below code snippet you can manage it.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {       
        if (e.Item is GridPagerItem)
        {
            RadComboBox pageSize = (RadComboBox)e.Item.FindControl("PageSizeComboBox");
  
            // You can also access your pagesize combobox here
            // And add or Remove item as per your requirement.
            pageSize.Items[0].Remove();
        }
    }


Thanks,
Jayesh Goyani
0
golddog
Top achievements
Rank 1
answered on 06 Feb 2012, 07:01 PM

The problem is on the second ajax postback.  In my test, we first query for 50 items (grid's markup page size), set those to DataSource and update VirtualItemCount, so we see 2839 items in 57 pages.

Change the page size to 200, which causes:

  • ItemCreated fires.  pager has three items, 10, 20, 50, we add 100 and 200.  Grid's PageSize is 50.
  • NeedDataSource fires.  Run a query for 200 items, set them to DataSource, update VirtualItemCount.
  • ItemCreated fires.  Pager now has four items: 10, 20, 50, and 200. 
    • We add 100 and 200. 
    • Both pager's SelectedValue and Grid's PageSize are 200.
    • We now show 2839 items in 15 pages.

Now change the page size to 100:

  • ItemCreated fires.  Pager has four items: 10, 20, 50, 200.  We add 100 and 200.  Grid's page size is 200.
  • NeedDataSource fires.  We query for 100 records, set them to DataSource, update VirtualItemCount.
  • ItemCreated fires.  Pager is back to four items: 10, 20, 50, 200. 
    • We add 100 and 200.
    • pager's SelectedValue is (inexplicably) 10.
    • Gird's PageSize is 100
    • pager's SelectedValue doesn't accept the setting from grid's PageSize and stays at 10.
    • The grid continues to show 2839 items in 15 pages and the 200 entries bound previously, despite us just setting DataSource to a list containing 100 entries.

So it seems to me that there's a problem with the state of the GridPagerItem relative to custom paging and multiple ajax postbacks.

0
golddog
Top achievements
Rank 1
answered on 06 Feb 2012, 10:38 PM
I had another idea, and ran some tests adding each of 100 and 200 to the page size items independently.  Each of those tests worked great.

However, when I add them both to the pager, that seems to screw things up.  That's when the paging seems to get "stuck". 

Note that it's on an ajax postback which is subsequent to picking a custom page size.  The first time I go custom, whether I pick 100 or 200, shows the appropriate amount of data.

On the next page size change, despite the grid's PageSize property changing, and the right number of items being set in the grid's data source, the grid continues to show the data and counts from the second postback. 

It does seem to be related to using one of the custom page sizes.  I can flip around between 10, 20, 50 all I want.  But, once I've hit 100 or 200, then do another page size, going back to 100 or 200 leaves me with the previous grid display.

It seems kind of as if re-adding the custom page sizes on the ajax postback doesn't get handled quite right (?).
0
golddog
Top achievements
Rank 1
answered on 06 Feb 2012, 11:55 PM
It finally occurred to me to try setting SelectedIndex instead of SelectedValue in debug, just as a test.

It turns out to be blindingly simple (as most things are): the constructor for RadComboBoxItem with one string leaves the value os empty string.  So, when I went to set the RadComboBox's SelectedValue to the Grid's PageSize, it didn't have an Item with Value == 100.

Correcting my constructor seems to have it working.  I need to do a little jumping around since the grid is "remembering" 200 when I select it, so I add 100 back in the right place, but that shouldn't be too much.

Thanks for the ideas.

Scott
0
Darren
Top achievements
Rank 1
answered on 28 Apr 2012, 04:26 PM
You're absolutely right golddog!  Changed to:

...
pageSize.Items.Add(new RadComboBoxItem("100", "100"));
...


and that made it work.

Darren
Tags
Grid
Asked by
golddog
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
golddog
Top achievements
Rank 1
Darren
Top achievements
Rank 1
Share this question
or