Document directory
- Introduction
- Important attributes
- Example 1
- Example 2
Introduction
We can see from the name that this is a menu control. When you move the mouse to a specified position, a menu appears without affecting other elements and allows you to perform some operations.
Important attributes
Targetcontrolid: ID of the target control to display the menu
Popupcontrolid: ID of the control for the pop-up menu
Hovercssclass: Specifies the style of the target control when you move the cursor to the target control.
Popupposition: Position of the pop-up menu. There are five values (bottom, top, left, right, center)
Offsetx: the position of the menu from the target control-abscissa
Offsety: the position of the menu from the target control-y coordinate
Popdelay: the delay time of the pop-up menu
Example 1
1. Open vs2005 and create an ajaxcontroltoolkit website named oec2003_hovermenu.
2. Open the default. aspx page and switch to the design view.
3. Add a textbox and a panel control to the page, and add three linkbutton controls to the panel control, as shown below:
<Asp: Panel id = "Panel1" runat = "server" Height = "50px" width = "125px"> <asp: linkbutton id = "linkbutton1" runat = "server" onclick = "linkbutton1_click"> date </ASP: linkbutton> <br/> <asp: linkbutton id = "linkbutton3" runat = "server" onclick = "linkbutton3_click"> time </ASP: linkbutton> <br/> <asp: linkbutton id = "linkbutton2" runat = "server" onclick = "linkbutton2_click"> name </ASP: linkbutton> </ASP: Panel>
4. Add the hovermenu control on the page and set its properties. The Code is as follows:
<ajaxToolkit:HoverMenuExtender ID="HoverMenuExtender1" runat="server" TargetControlID="TextBox1" OffsetX="-1" OffsetY="-1" HoverCssClass="hover" PopupControlID="Panel1" PopupPosition="bottom"></ajaxToolkit:HoverMenuExtender>
5. Switch to the Code view and add a CSS style between the head elements to implement the style when the mouse moves to the target control, as shown below:
<style type="text/css"> .hover { background-color:blue; background-repeat:repeat-x; background-position:left top; } </style>
6. Three linkbutton controls are placed in the Panel. After you click the linkbutton control, the corresponding content is displayed in the text box. The event code for clicking a linkbutton is as follows:
protected void LinkButton1_Click(object sender, EventArgs e){ TextBox1.Text = DateTime.Now.ToShortDateString();}protected void LinkButton3_Click(object sender, EventArgs e){ TextBox1.Text = DateTime.Now.TimeOfDay.ToString();}protected void LinkButton2_Click(object sender, EventArgs e){ TextBox1.Text = "oec2003";}
7. Save the design and run the program. When you move the mouse over the text box, you will find that the text box style is changed to our custom style, and a menu is displayed, that is, the panel control.
Click the button in the menu and the corresponding content will appear in the text box.
Example 2
The preceding is a simple application of the hovermenu control. In fact, the hovermenu control can also be used to replace the editing function in the gridview control so that the editing column can appear in our custom menu, let's take a look at how it is implemented.
1. Create a new web page on the website named oec2003_gridviewhovermenu.aspx
2. Switch to the design view and add the scriptmanager and updatepanel controls to the page.
3. Add the gridview and sqldatasource controls in the updatepanel control and configure the data source, as shown below:
<asp:sqldatasource id="SqlDataSource1" runat="server" conflictdetection="CompareAllValues" connectionstring="<%$ ConnectionStrings:studentConnectionString %>" deletecommand="DELETE FROM [studentInfo] WHERE [stuID] = @original_stuID AND
[stuName] = @original_stuName" insertcommand="INSERT INTO [studentInfo] ([stuID], [stuName]) VALUES (@stuID, @stuName)" oldvaluesparameterformatstring="original_{0}" selectcommand="SELECT * FROM [studentInfo]" updatecommand="UPDATE [studentInfo] SET [stuName] =
@stuName WHERE [stuID] = @original_stuID AND [stuName] = @original_stuName"><DeleteParameters> <asp:Parameter Name="original_stuID" Type="String" /> <asp:Parameter Name="original_stuName" Type="String" /></DeleteParameters><UpdateParameters> <asp:Parameter Name="stuName" Type="String" /> <asp:Parameter Name="original_stuID" Type="String" /> <asp:Parameter Name="original_stuName" Type="String" /></UpdateParameters><InsertParameters> <asp:Parameter Name="stuID" Type="String" /> <asp:Parameter Name="stuName" Type="String" /></InsertParameters></asp:sqldatasource>
4. Open the edit column window of the gridview control and set the stuname field to the template column.
5. Implement two menus in the gridview control. One is "edit" and "delete" in normal mode, and the other is "Update" and "cancel" in the editing status"
6. When editing the template column, you will find two items in the window: itemtemplate and edititemtemplate. The hovermenu and panl controls are added under the two items respectively. Two linkbutton controls are placed in each panel control. The attribute settings are as follows:
<edititemtemplate> <asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("stuName") %>'></asp:TextBox> <ajaxToolkit:HoverMenuExtender ID="HoverMenuExtender2" runat="server"
TargetControlID="TextBox1"
PopupControlID="Panel2" OffsetX="10" OffsetY="-1" PopupPosition="right"> </ajaxToolkit:HoverMenuExtender> <asp:Panel ID="Panel2" runat="server" Height="50px" Width="125px"> <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Update"
Commandname = "Update"> Update </ASP: linkbutton> <asp: linkbutton id = "linkbutton4" runat = "server" commandargument = "cacel"
Commandname = "cancel"> cancel </ASP: linkbutton> </ASP: Panel> </edititemtemplate> <itemtemplate> <asp: label id = "label1" runat = "server" text = '<% # BIND ("stuname") %>'> </ASP: Label> <ajaxtoolkit: hovermenuextender id = "hovermenuextender1" runat = "server"
TargetControlID="Label1" PopupControlID="Panel1" OffsetX="10" OffsetY="-1"
PopupPosition="right"> </ajaxToolkit:HoverMenuExtender> <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px"> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="Edit"
Commandname = "edit"> edit </ASP: linkbutton> <asp: linkbutton id = "linkbutton2" runat = "server" commandargument = "delete"
Commandname = "delete"> Delete </ASP: linkbutton> </ASP: Panel> </itemtemplate>
Note that because linkbutton needs to edit the data row of the gridview, The commandargument and commandname attributes of the linkbutton must be set to the name of the corresponding gridview method, for example, the commandname corresponding to the editing function should be set to edit.
7. Save the design and run the program. When you move the cursor over the data row in the stuname column, a custom menu will appear, enabling editing. The function is the same as enabling editing in the gridview.