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

Context Menu Cancel Postback in OnClientItemClick

6 Answers 236 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Josh Sudbury
Top achievements
Rank 1
Josh Sudbury asked on 08 Apr 2010, 04:03 PM
I am trying to cancel the postback of an onItemClick command inside of an onClientItemClick command.

 <script type="text/javascript">  
        function onMenuItemClicked(sender, eventArgs) {  
            alert("Testing");  
            return false;  
        }  
</script> 
 
<telerik:RadContextMenu ID="menuContextMenu" runat="server" OnClientItemClicked="onMenuItemClicked" 
                                        onitemclick="menuContextMenu_ItemClick">  
    <Items> 
       <telerik:RadMenuItem Text="Add Sub Menu" Value="1"></telerik:RadMenuItem> 
       <telerik:RadMenuItem Text="Remove" Value="2"></telerik:RadMenuItem> 
    </Items> 
    <Targets> 
       <telerik:ContextMenuTagNameTarget TagName="menuNode" /> 
    </Targets> 
</telerik:RadContextMenu> 

The javascript runs properly as I get the alert but the post back still occurs even though the function returned false.

I have also tried changing the OnClientItemClicked to  "return onMenuItemClicked();" but this breaks the RadContextMenu all together and I get errors on the page due to improper syntax.

I have also tried adding "window.event.returnValue = false;" to the javascript but have also had no luck with that.

Any help would be greatly appreciated.

6 Answers, 1 is accepted

Sort by
0
Josh Sudbury
Top achievements
Rank 1
answered on 08 Apr 2010, 05:27 PM
Well it looks like I didn't search thouroughly enough and I found my answer this this forum post.

http://www.telerik.com/community/forums/aspnet-ajax/menu/cancel-menu-postback-clientside.aspx

Sorry for the double post!
0
Piyush
Top achievements
Rank 1
answered on 30 Sep 2011, 12:27 PM
The solution doesnot work for me. I have been searching for this over the internet and tried almost all the combinations. Following is the javascript I used (contains all the combinations):

function testRowClick(sender, e) { 
           e.set_cancel(true); 
           if (!e) var e = window.event; 
           e.cancelBubble = true
           if (e.stopPropagation) e.stopPropagation(); 
           window.event.cancelBubble = true
           e.cancelBubble = true
           e.returnValue = false
           if (e.stopPropagation) { 
               e.stopPropagation(); 
               e.preventDefault(); 
           }    
       }

aspx:

<telerik:radgrid ID="radGridEmailDetails" runat="server" AllowPaging="True"  AllowSorting="True" AutoGenerateColumns="False" GridLines="Both" PageSize="12"  EnableEmbeddedSkins="false" Skin="HDGrid" OnItemCommand="radGridEmailDetails_ItemCommand" OnItemDataBound="radGridEmailDetails_ItemDataBound"  >
                                    <ClientSettings EnablePostBackOnRowClick="true">
                                        <Selecting AllowRowSelect="true" />
                                    </ClientSettings>
                                      
                                    <HeaderContextMenu EnableAutoScroll="True">
                                    </HeaderContextMenu>
                              
                                    <PagerStyle Mode="NextPrevAndNumeric" NextPageImageUrl="~/Images/pagination_next.gif" HorizontalAlign="Center"
                                      PrevPageImageUrl="~/Images/pagination_prev.gif" LastPageImageUrl="~/Images/pagination_last.gif" 
                                      FirstPageImageUrl="~/Images/pagination_first.gif" ShowPagerText="false" Position="Bottom"/><SortingSettings EnableSkinSortStyles="False" />
                                      
                                    <HeaderStyle HorizontalAlign="Center"/>
                                      
                                    <ItemStyle HorizontalAlign="Center" />
                                      
                                    <AlternatingItemStyle HorizontalAlign="Center" />
                                    <%--<PagerStyle Mode="NextPrev" Position="TopAndBottom" />--%>
                                      
                                    <MasterTableView GroupLoadMode="Server" TableLayout="Fixed" Width="100%">
                                        <Columns>                             
                                            </Columns>
                                    </MasterTableView>
                                </telerik:RadGrid>

aspx.cs:

radGridEmailDetails.ClientSettings.ClientEvents.OnRowClick = "testRowClick";

Any advice would be appreciated. TIA
0
Helen
Telerik team
answered on 30 Sep 2011, 01:27 PM
Hello Piyush,

If I understand you correct you want to cancel the postback when the grid row is clicked?

Kind regards,
Helen
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
0
Piyush
Top achievements
Rank 1
answered on 03 Oct 2011, 04:28 AM
Yes, you are right. On RadGrid.ItemCommand there are a few javascript validations, if those fail I want to cancel the ItemCommand event of the RadGrid. And I also looked into the possibility of trigerring ItemCommand event from javascript if the validations pass but that is not feasible in my situation.

Regards,
Piyush.
0
Shinu
Top achievements
Rank 2
answered on 03 Oct 2011, 06:01 AM
Hello Piyush,

You can explicitly fire the command by using the fireCommand event. Here is the documentation which explains the same.
fireCommand.

Thanks,
Shinu.
0
Piyush
Top achievements
Rank 1
answered on 03 Oct 2011, 09:46 AM
Hello Shinu,

Thanks for the response, it helped. I am able to workaround the problem for now (with some hardcoding :( ). But I am still looking for a way where I can stop a postback from javascript like we can do for normal asp controls like asp Buttons.

Regards,
Piyush Agrawal.
Tags
Menu
Asked by
Josh Sudbury
Top achievements
Rank 1
Answers by
Josh Sudbury
Top achievements
Rank 1
Piyush
Top achievements
Rank 1
Helen
Telerik team
Shinu
Top achievements
Rank 2
Share this question
or