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

ContextMenu Right click in a Listbox

2 Answers 98 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Robert Canal
Top achievements
Rank 1
Robert Canal asked on 08 Feb 2011, 09:32 PM
When you right click on an item in the ListBox it works as expected.  But if you right click in the ListBox but not on an item it gives me the following error any ideas on how to correct this?

"Microsoft JScript runtime error: 'get_item()' is null or not an object"

 

function showContextMenu(sender, e) {

 

 

var RadList = $find("Listbox");

 

 

var RadListSelected = RadList.get_selectedItem();

 

 

var menu = $find("cm1");

 

 

var rawEvent = e.get_domEvent().rawEvent;

 

menu.show(rawEvent);

e.get_item().select();  < ERROR is called on this item because object is nothing

$telerik.cancelRawEvent(rawEvent);

}

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 Feb 2011, 06:25 AM
Hello Robert,

I suppose you are showing the ContextMenu based on the clicked menu item. So if you right click on the ListBox other than an item, there won't be any item and e.get_item() will be null, that's why the error comes. So one suggestion is to check whether e.get_item() is null or not, before showing the ContextMenu. Here is a sample code.

<script type="text/javascript">
 function showContextMenu(sender, e)
  {
         if (e.get_item() == null)
          {
            e.get_domEvent().preventDefault();//prevents showing the browser menu
            return;
         }
       var menu = $find("<%= cm1.ClientID %>");
       . . . . . . . . . . . . .
 }
</script>

Thanks,
Shinu.
0
Robert Canal
Top achievements
Rank 1
answered on 09 Feb 2011, 02:54 PM
That was what I need thanks....I am very new to javascript so I wasn't aware of the " e.get_domEvent().preventDefault();//prevents showing the browser menu  " event.

Tags
ListBox
Asked by
Robert Canal
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Robert Canal
Top achievements
Rank 1
Share this question
or