Add shortcut menu for ASP. NET Control

Source: Internet
Author: User

  1. ContextMenu Control shortcut menu Control Overview:

    MSDN Liabrary contains several DHTML shortcut menu examples. This feature cannot be implemented. A shortcut menu is a set of tag code at any position on the page. It consists of two parts: the interface and script (associate the UI when you right-click the page). The UI provides a list of clickable elements-menu items-and their respective content text, icon, command name, target url, tooltip, and everything you think is necessary. The shortcut menu interface is part of a page and is a member of the page Control tree. (Too many shortcut menus may cause execution problems: ASP.. NET sends more shortcut menus to the browser.) When you right-click an element on the page, a JavaScript code is triggered. This script moves the shortcut menu to the right-click position for display.

    The shortcut menu is hidden. The shortcut menu is displayed only when you right-click an element bound to a shortcut menu on the page. The page element receives the script event and a shortcut menu is displayed as a response. The script event depends on the browser and is an OnContexMenu event in Internet Explorer 5.0 and later versions. in Netscape6.0 and later versions, you need to use the OnMouseUp event (in Internet Explorer 5.0 +, you may also use the OnMouseUp event, but you need to write a few more lines of code) in the script that receives the event, obtain the UI code block of the shortcut menu and move it to the position where the shortcut menu is clicked. Set the visibilty attribute of the pop-up panel. When you click a menu item, the page will return and cause server events. For servers, clicking a menu item is no different from clicking a regular button.

    What should I do when a user wants to cancel a shortcut menu that has already been opened? In windows, you can click Esc or click it outside the menu area to cancel the shortcut menu that has been popped up. Therefore, you must implement this function on the Web. Note that the Esc key is available only when the shortcut menu is active (displayed. Because Esc can also be used for other keyboard shortcuts of other elements.

    You can also hide the menu when you move the mouse away. You can use scripts to operate OnMouseLeave events.

  2. Use the pop-up menu control

    Suppose we have designed this pop-up menu (we will talk about how to implement this shortcut menu control later), then how to use it in ASP. NET page to add one or more shortcut menus? First, in Visual Studio®. NET drag one or more shortcut menu controls to the page. Then, add menu items for each menu, and configure the tooltip, command name, and other required content for each menu item, such as the shortcut key and help topic link. Command name is used to determine which menu item is clicked when a page is returned when a shortcut menu is clicked; the menu item set of each shortcut menu control instance must be unique.

    You must add code to the OnContextMenu event of the HTML tag to bring up the shortcut menu. The code must trust a series of parameters, such as the x, y coordinates and clicked elements, and the instance of the shortcut menu to be used. NOTE: If necessary, you can use this method to completely replace the shortcut menu of the browser. The JavaScript code bound to the OnContextMenu event is dynamically generated at runtime. The ContextMenu control exposes a set attribute that contains the set of controls that bind shortcut menus. The ContextMenu control will give these controls an oncontextmenu attribute at runtime. OK! Done! You can test it.

    Let's take a look at the binding mechanism between the ContexMenu control and any elements that display shortcut menus on the page. How does this Mechanism support the design? Ideally, a ContextMenuId attribute is directly exposed based on each Web control of the base class. When this attribute is selected in the Properties window, the ContextMenu control list is displayed on the page. Of course, we have created these ContextMenu controls. The ContextMenuId attribute is not supported in ASP. NET 1. x and will not be supported in future ASP. NET2.0.

    In the integrated development environment of Visual Studio. NET 2003, ASP. NET composite controls can accomplish this well. You can use the class to write and combine existing controls to create new controls. The composite control displays a user interface that allows you to reuse the functions of an existing control. The composite control can combine attributes from the properties of the Child control and display events triggered by the Child control. It can also expose custom attributes and events.

    I do not choose to use composite controls for the following reasons: one is Visual Studio. NET 2003 does not support extended controls in Web forms (see Extender provider components in ASP. NET: an IExtenderProvider implementation), second, in Visual Studio 2005 ASP. the component tray area is no longer supported in the. NET designer. The Web forms designer currently only supports ASP. NET controls, while ignoring non-Visualized components such as composite controls. Visual Studio 2005 will no longer trust the InitializeComponent section, and will no longer add any tool-generated code in the code file. The ASP. NET Control is not designed to have a shortcut menu. Therefore, to bind a shortcut menu, you can only execute it through the shortcut menu control. Here I use a form similar to ASP. NET authentication control and the verified Control Association.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.