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

RadGrid Paging breaks CommandName and CommandValue inside ItemCommand or OnClick postbacks.

6 Answers 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Desiree
Top achievements
Rank 1
Desiree asked on 16 Apr 2011, 02:45 AM
I have a simple RadGrid, AllowPaging="true" and data is bound via OnNeedDataSource. The following relevant template to describe the problem is:

<telerik:GridTemplateColumn HeaderText="Actions">
    <ItemStyle Wrap="false" />
    <ItemTemplate>
        <asp:Button ID="uxDeleteDeployment" runat="server" CommandName='<%# Eval("deploymentId") %>' ToolTip="Delete deployment" AlternateText="Delete deployment" ImageUrl="/_layouts/images/delete.gif" ImageAlign="AbsMiddle" Visible="false" OnClick="uxDeleteDeployment_Click" OnClientClick='<%# JsEncoder.Format(@"alert(""{0}""); return true", Eval("deploymentId")) %>' />
    </ItemTemplate>
</telerik:GridTemplateColumn>

The OnClientClick code alert the value passed to it (in this case it is just a deploymentId -- the same binding that is used with CommandName). The importance of this will be explained below. Also note that I do not set CommandArgument so it will default to the "data key" which is the deploymentId.

In the uxDeleteDeployment_Click there is the following code:

var button = (Button)_sender;
throw new InvalidOperationException("Foo: " + button.CommandName + "|" + button.CommandArgument);

Now, there are the symptoms:

When an item on the FIRST PAGE is clicked the confirm dialog will show X (X is a particular but arbitrary deployment ID for the item) and the exception thrown on on the post-back is "Foo: X|X" -- this is correct behavior.

Now, when the SECOND PAGE is paged to and the button is clicked the confirm dialog will show X (X is a particular but arbitrary deployment ID for the item). However, the exception thrown on the post-back is "Foo: Y|Y" (note Y and not X!!!) where Y is the deploymentId of the item with the SAME PAGE POSITION but on the FIRST PAGE. For instance, Y is the deploymentId of Item #3 from the data-source if Item #13 (#3 in list on the SECOND PAGE) is clicked -- this is not correct behavior.

Additionally,
If the page-size is changed (default is 10) to 20 and item #13 is clicked, the OnClick callback will not be invoked -- rather the page will be refresh to show only the FIRST PAGE (page size of 10) but leaves "20" in the page-size field -- this is not correct behavior.

Other notes:
  • BOTH issues do not exist if paging is disabled.
  • BOTH issues do not occur if manually intercepting the client click and invoking an alternate post-back. (The issue seems to be entirely contained with the post-back of the RadGrid itself.)
  • Using version 2011.1.413.35 (latest as of today) but exact same problems(s) on 2010 Q3
  • ViewStateMode does not change the symptoms/behavior. (ViewState is enabled because filtering is used; disabling the ViewState was just for a test and it had no noticeable effect.)
  • CommandName/CommandArgument have this same incorrect behavior inside of an ItemCommand for the grid (in the case above I discuss it in context of an OnClick applied to the individual button). In the case of ItemCommand the code 'e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["deploymentId"];' results in the same value as CommandArgument (which is wrong).
  • Removing the client-side event handler does not correct the behavior.
  • The Eval is bound correctly per (as shown in the JS alert) but the values sent to the post-back callback were wrong.
  • No AJAX is used -- all post-backs are normal HTTP. There are no additional/secondary post-backs that occur (verified with FireBug).
  • The RadGrid is running in a UserControl a SharePoint 2010 environment. This should not matter.
  • (The text formatting of this post input area changed when a new item was added to the list and some of the text now looks ugly.)

I have looked through the Demos and I can not find a case covering this use-case -- a custom OnClick/ItemCommand of a Particular Item being manually handled from the non-first page of a paged RadGrid control (The "Selected" demos do not count because the behavior is internal to the RadGrid control and not a manual event). It would be nice to see a test-case demonstrating this scenario as then I could look for fault elsewhere. However, as it is now, I cannot find this example demonstrated.

Any suggestions/fixes are appreciated.

Because of this error I must now go and validate all existing code to ensure this issue is not present elsewhere. Not a good feeling.

6 Answers, 1 is accepted

Sort by
0
Desiree
Top achievements
Rank 1
answered on 16 Apr 2011, 09:30 AM
The closest I can find to this is NeedDataSource called before Item Command 
and NeedDataSource fires on every postback

In my cases these are the observed behavior and seems to be a plausible explanation for the other behavior.

Like the authors of the posts, I can isolate that is NOT related to a disabled view-state. However, there is nothing fired from within Page_Load and there is no traversal of the controls in my code -- yet each post-back OnNeedDataSource is triggered. (Being called from "external code" I can't trace into it.)

The RebindReason is always "InitialLoad", which seems odd, but agrees with the 2nd link. I am not iterating any controls or doing any work in Page_Load or PreInit, etc... the RadGrid is, however, in a custom User Controller/ASCX which is dynamically added.
0
Desiree
Top achievements
Rank 1
answered on 16 Apr 2011, 09:24 PM
Okay, I solved the problem. Finally. The issue was the dynamic user-control was being removed and re-added each request. Go broken legacy code.

However, I would like to submit a request for "RadGrid Fail Fast" in this scenario -- that is, if a ItemCommand (or OnClick, if that's possible to detect) is scheduled to be triggered after an 'InitialRebind' (or an 'InitialRebind' is somehow triggered after the control/state is initially setup, I am not limiting the exact scope) then the RadGrid SHOULD throw an exception as the state and behavior of the RadGrid is now undefined and unreliable and the core issue must be dealt with. (This unreliable behavior is pointed out in my posted and the linked in posts).
0
Veli
Telerik team
answered on 21 Apr 2011, 10:15 AM
Hello Desiree,

Glad to hear you were able to pinpoint the cause of this issue. The main argument indicating improper control life cycle is the NeedDataSource event getting fired on every postback. In a regular page life cycle with enabled ViewState, RadGrid does not rebind on every postback. This can be verified by submitting the page from an external button for example. If no operation requiring explicit rebind is invoked, RadGrid loads its data cells from the ViewState and does not explicitly rebind. Databinding operations, in turn, are paging, sorting, filtering, data editing and some others explained in this online help topic. Unless any of these operations are triggered, or you explicitly rebind RadGrid, the latter should not fire the NeedDataSource event for the current postback. The fact that RadGrid in your scenario was databinding on every postback (no matter the control or command invoking it) indicates an issue with the control life cycle.

As for your request for detecting a potential fail scenario for RadGrid, I have notified our dev team on that. Note, however, that there may be valid scenarios where RadGrid rebinds before firing ItemCommand. One of these cases is with disabled ViewState. When ViewState is disabled, RadGrid cannot recreate its structure from the ViewState and needs to rebind in the Init phase (so that control tree is constructed as it was at the end of the previous page life cycle). There may be other scenarios where you may need to rebind RadGrid earlier than expected. You will agree that anticipating every possible working scenario where RadGrid should or should not "fail fast" is unreasonable given the enormous set of potential deployment scenarios a server control may participate in. For this particular reason, the support staff at telerik is ready to assist you with your particular scenario trying to identify whether it is supported, recommended, or else, what is the optimal setup for the components you are using in your business scenario.

Regards,
Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
ryan
Top achievements
Rank 1
answered on 18 Jan 2012, 05:07 PM
Hey Desiree,  So you fixed this by not re-adding the user control for each request?  If the user control is dynamically added to begin with, how'd you get away with doing that?
0
L
Top achievements
Rank 1
answered on 16 May 2012, 02:19 PM
Hello Veli,

I was about to ask a question and I read this post. I have decided to ask you directly because you are very familiar with my scenario.

I have a radgrid with EnableViewState=false and with sorting enable. The NeedDataSource event is fired before the ItemCommmand and I'm loosing the sort I gave to my grid, which causes in the item command I get the wrong Item and the list to be unsorted. 
Steps to reproduce: Having a radgrid EnableViewState=false, AllowSorting=true
X
Y
Z
1- Sort the grid by any column.
Y
Z
X
2- Click a button to modify X which is in the third position now. The sequence of events is as follow:
NeedDataSource - HERE I lose the sort I gave to my grid so it is back to 
X
Y
Z
ItemCommand - HERE I get Z, the current item in the third position, but I was expecting X.

After that, the grid is unsorted again.

I would like a work around for
1- Get in the ItemCommand event the correct item.
2- Have the grid sorted as it was before. 

Thanks in advance for your help.  
0
Radoslav
Telerik team
answered on 21 May 2012, 07:22 AM
Hello L,

I tried to reproduce the described issue but to no avail, when I disable the Page’s ViewState the paging of the RadGrid works as expected. I am sending you a simple example which demonstrates the desired functionality. Please check it out and let me know what differs in your case.
Additionally if you use custom sorting could you please verify that you Applying Custom Sort Criteria as it shown in the following online documentation article:
http://www.telerik.com/help/aspnet-ajax/grid-apply-custom-sort-criteria.html

Looking forward for your reply.

Regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Desiree
Top achievements
Rank 1
Answers by
Desiree
Top achievements
Rank 1
Veli
Telerik team
ryan
Top achievements
Rank 1
L
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or