Context Menu Cancel Postback in OnClientItemClick

7 posts, 0 answers
  1. Josh Sudbury
    Josh Sudbury avatar
    10 posts
    Member since:
    Apr 2010

    Posted 08 Apr 2010 Link to this post

    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.
  2. Josh Sudbury
    Josh Sudbury avatar
    10 posts
    Member since:
    Apr 2010

    Posted 08 Apr 2010 Link to this post

    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!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Piyush
    Piyush avatar
    3 posts
    Member since:
    Sep 2011

    Posted 30 Sep 2011 Link to this post

    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
  5. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 30 Sep 2011 Link to this post

    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
  6. Piyush
    Piyush avatar
    3 posts
    Member since:
    Sep 2011

    Posted 02 Oct 2011 Link to this post

    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.
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Oct 2011 Link to this post

    Hello Piyush,

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

    Thanks,
    Shinu.
  8. Piyush
    Piyush avatar
    3 posts
    Member since:
    Sep 2011

    Posted 03 Oct 2011 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017