Add a shortcut menu for the ASP.net control (1)

Source: Internet
Author: User
Asp.net| Menu | Controls add shortcut menus to asp.net controls (1)
Although shortcut menus are already a very common element in desktop applications, the shortcut menus are not very common in Web applications because of the lack of a good description of server-based technologies like asp.net. To use the shortcut menu, the browser must highly support DHTML and rich event patterns, such as Microsoft®internet Explorer 5.0 and later, or Netscape 6.0. However, the object mode of various browsers, although functionally large, has different names, which requires us to implement the transformation of objects and events from one pattern to another.

Here, we create a pin to Internet Explorer's Object mode asp.net shortcut menu. Next, I will modify the functionality to support other browsers. The code supports ASP.net 1.x, or it can be compiled under asp.net2.0 Beta1 Outline of the
ContextMenu Control shortcut Menu Controls Overview:
The MSDN liabrary contains examples of several DHTML shortcut menus. Provides an inability to implement this functionality separately. A shortcut menu is a set of markup codes anywhere on the page. It consists of two parts-the interface and the script (when the user correlates the UI when the page is clicked), the UI provides a list of clickable elements-menu items-and their respective content texts, icons, command names (commands name), Target URLs, hints (tooltip), And all the things you think you need to show up. The shortcut menu interface is part of a page and belongs to a member of the page control tree. (Too many shortcut menus will have a problem with execution: ASP, compared to the general situation of most users.) NET will send more shortcut menus to the browser when the user right-clicks on one element of the page, a JavaScript is raised, which moves the shortcut menu to the right click position.

The shortcut menu remains hidden, and the shortcut menu appears only when the user right-clicks the element on one of the page's bound shortcut menus. The page element receives a script event and pops up a shortcut menu in response. Script events are trusted by browsers and are Oncontexmenu events in Internet Explorer 5.0 and later versions. In Netscape6.0 and later versions you will use ONMOUSEUP events (in Internet Explorer 5.0+ You may also use the OnMouseUp event, but write a few more lines of code. In the script that receives the event, get the UI code block of the shortcut menu and move it to the location where the click occurred. Also sets the Visibilty property of the Panel that pops up. When a user clicks on a menu item, the page is returned and a server-side event is raised. For servers, it's no different from clicking a menu item and clicking on a regular button.

What happens when a user wants to cancel a shortcut menu that is already open? In a Windows program, you can cancel a pop-up shortcut menu by pressing the ESC key or clicking outside the menu area. So you have to implement this feature on the Web. Note that the ESC key is only available when the shortcut menu is active (displayed). Because ESC is also available for other keyboard shortcuts to other elements.

I can also have the menu hidden when the user moves the mouse over the menu. The OnMouseLeave event implementation can be done by scripting.
Using the pop-up menu control
Assuming we've designed this pop-up menu (we'll talk about how to implement the shortcut menu control in a moment), how do you use it to add one or more shortcut menus to the asp.net page? First, in Visual studio®. NET, drag one or more shortcut menu controls to the page. You then add menu items to each menu and configure the ToolTip for each item, the command name and other required content, such as a link to a shortcut key and Help topic. Command name is used to determine which menu item is clicked when a page is returned in response to the click of the shortcut menu, and it must be unique to the menu item collection for each instance of the shortcut menu control.

You must add code to the OnContextMenu event of the HTML tag to eject the shortcut menu, the code must rely on a series of parameters, like the x,y coordinates clicked, the clicked element, and the instance of the shortcut menu to use. Note: If necessary you can use this way to completely replace the browser's shortcut menu. The JavaScript code that is bound to the execution of the OnContextMenu event is generated dynamically at run time. The ContextMenu control exposes a collection property to contain a collection of controls that are bound to the shortcut menu, and the ContextMenu control will give the control a OnContextMenu property at run time. Ok! is finished, you can test it.

Wait, let's think about the binding mechanism for Contexmenu controls and any elements in the page that display shortcut menus, how does this mechanism support design-time? Ideally, a Contextmenuid attribute is exposed directly to each Web control of the base class. Then, when you select this property in the Properties window, you will see a list of the ContextMenu controls in the page. Of course these ContextMenu controls are already created, Contextmenuid properties are not supported in ASP.net 1.x, and are not supported in future asp.net2.0.

In the visual Studio. NET 2003 development Environment, ASP. NET composite control can do this work well. You can author new controls by combining existing controls by using class composition. A composite control can render a user interface that uses the functionality of an existing control. A composite control can synthesize properties from a child control's properties and cause events that are raised by reason child controls. It can also expose custom properties and events.

I don't choose to use a composite control for several reasons: one is Visual Studio. NET 2003 is not good for extended support for control features in Web Forms (see: Extender Provider components in ASP.NET:an IExtenderProvider implementation), and second, the component tray area is no longer supported in the ASP.net designer of Visual Studio 2005. The Web Forms Designer now supports only asp.net controls and ignores non-visual components such as composite controls. Visual Studio 2005 will no longer trust the InitializeComponent section and no longer automatically add any tool generation (tool-generated) code to the code file. Asp. NET control is not designed to have a shortcut menu, so you can bind a shortcut menu only through the shortcut menu control itself. Here I use the form of an association between a asp.net validation control and a validated control.


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.