Ticket T212881
Visible to All Users

GridView ContextMenu Event fires too fast - How to hide particular Item based on Row Values

created 10 years ago

Dear Devexpress,
I have implemented your Gridview control into my ASP.NET MVC web application and I have a problem with the context menu.
I have to turn the visibility off, on some of the context menu options depending on a model property.
Here is my example, to clarify:
I added two items to the context menu:
settings.ClientSideEvents.ContextMenu = "OnContextMenuInit";
settings.FillContextMenuItems = (sender, e) =>
{
e.Items.Clear();
e.Items.Add("Módosítás", "modosit");
e.Items[0].Image.Url = "~/Content/images/edit.png";
e.Items[0].Image.Width = 20;
e.Items[0].Image.Height = 20;
e.Items.Add("Készrejelentés", "keszrejel");
e.Items[1].Image.Url = "~/Content/images/commit.png";
e.Items[1].Image.Width = 20;
e.Items[1].Image.Height = 20;
} depending on a model property I would like to hide the second option, before the context menu appears:
(I'm currently trying via javascript, I have implemented the context menu client side event, to catch the context menu init)
function OnContextMenuInit(s, e) {
if (e.objectType == "row") {
JuttKarbGrd.GetRowValues(e.index, 'n_sortipus;', function (data) {
var tipus = data[0];
e.menu.GetItemByName('keszrejel').SetVisible(tipus == 1);
});
}
}

This piece of code turns the option's visibility off, BUT the context menu appears before the end of the ContextMenu event.
I have clicked with right click on a row, where the n_sortipus not equals with 1, and I can see the whole context menu, before the code hides the second option.
1.jpg

After a second it disappears:
2.jpg

Is this a bug? Is there a solution for my problem?

Thanks,
Gergő

Answers approved by DevExpress Support

created 10 years ago (modified 10 years ago)

Hello Gergő,

The client-side ASPxClientGridView.GetRowValues returns results via a callback (i.e., it operates asynchronously).
So, the context menu is shown before the callback result is ready.

You can make the required field ("n_sortipus") a part of the GridViewSettings.KeyFieldName and use the client-side ASPxClientGridView.GetRowKey method instead (it works without a callback):

JavaScript
function OnContextMenuInit(s, e) { if (e.objectType == "row") { //JuttKarbGrd.GetRowValues(e.index, 'n_sortipus;', function (data) { //var tipus = data[0]; var tipus = s.GetRowKey(e.index); e.menu.GetItemByName('keszrejel').SetVisible(tipus == 1); //}); } }

If you need further assistance, please provide us with a sample project that illustrates your current progress.

    Show previous comments (1)

      Hello,

      >>Sadly, I have to use the GetRowKey method…

      I believe that you are talking about the GetRowValues method.

      >>Is there a way to solve this with using the callback function?

      You can make the required field ("n_sortipus") a part of GridViewSettings.KeyFieldName and use the client-side ASPxClientGridView.GetRowKey method instead, for example:

      C#
      settings.KeyFieldName = "REAL_ID_HERE;n_sortipus";
      JavaScript
      function OnContextMenuInit(s, e) { if (e.objectType == "row") { var tipus = s.GetRowKey(e.index).split('|')[1]; alert(tipus); e.menu.GetItemByName('keszrejel').SetVisible(tipus == 1); } }

        Thanks, it solved my problem!

          You are always welcome!

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.