Section 9.7. Adding Menus


9.7. Adding Menus

To add a menu to a web part, override CreateWebPartMenu to add MenuItem objects to the web part's MenuItems collection. For example, the following code adds a Save Settings item to the web part menu:

 Dim _mnuSave As MenuItem       Public Overrides Sub CreateWebPartMenu()           _mnuSave = New MenuItem("Save Settings", "mnuSave", _             New EventHandler(AddressOf _mnuSave_Click))            Me.WebPartMenu.MenuItems.Add(_mnuSave)       End Sub       Private Sub _mnuSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)          Try              Me.SaveProperties = Not _chkSave.Checked         Catch              _msg &= "You must be signed in as a contributor to save properties. "         End Try       End Sub 

The MenuItem constructor sets the displayed name , ID, and event handler for the menu item. In this case, the _mnuSave_Click event handler switches the SaveProperties setting for the web part on and off.

Menus don't save their state: they are redrawn from scratch each time the page is displayed. That's why a statement like _mnuSave.Checked = Not _mnuSave.Checked doesn't toggle the checkmark on a menu item. Instead you must use a web part property or a web control (such as the checkbox) to track the checked status of a menu item.

The CreateMenu and CreateChildControls methods are not called in a fixed order, so it's important to set web control properties in the OnPreRender event when working with web part menus. OnPreRender always occurs just before the web part is rendered. This code completes the Save Settings menu example by setting the control properties just before the web part is displayed:

 Dim WithEvents _cal As New Calendar      Dim _chkSave As New CheckBox      Dim _msg As String     Protected Overrides Sub CreateChildControls()         ' Add to controls collection         Controls.Add(_cal)         Controls.Add(New br)         Controls.Add(_chkSave)     End Sub     Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)          ' Set control properties          _chkSave.Text = "Save Settings"          _chkSave.Checked = Me.SaveProperties          _mnuSave.Checked = Me.SaveProperties          _cal.SelectedDate = Me.CalDate     End Sub     'Render this Web Part to the output parameter specified.     Protected Overrides Sub RenderWebPart _       (ByVal output As System.Web.UI.HtmlTextWriter)         ' Render view state controls.         RenderChildren(output)         ' Display message if error.         If _msg <> "" Then             output.Write("<br><strong>" & _msg & "</strong>")         End If     End Sub 

The preceding code creates a new br object to add a break to the controls collection. The br class inherits from the Literal class and is defined in the Ch09Samples project.


At runtime the web part includes a Save Settings item on its menu, as shown in Figure 9-12. If you don't want to display the checkbox, you can always set its Visible property to False .

animal 9-12. Adding a menu item to a web part

The Save Settings item appears whether or not the member is signed on. Since saving settings requires authentication, you may want to check the user 's status before adding the item, as shown here:

 Public Overrides Sub CreateWebPartMenu()          _mnuSave = New MenuItem("Save Settings", "mnuSave", _            New EventHandler(AddressOf _mnuSave_Click))  If Me.Context.User.Identity.IsAuthenticated Then _  Me.WebPartMenu.MenuItems.Add(_mnuSave)      End Sub 

The MenuItems collection Add method adds the item at the end of the menu. Use the Insert , Remove , and Replace methods to change the order of items. To create submenus, add the items to an existing item's MenuItems collection, as shown here:

 Public Overrides Sub CreateWebPartMenu()          ' Add multi-level menus          Dim mnuTop As New MenuItem("Top", "mnuTop", "")          Me.WebPartMenu.MenuItems.Add(mnuTop)          Dim mnuL1 As New MenuItem("Level 1", "mnuL1", "")          mnuTop.MenuItems.Add(mnuL1)          Dim mnuL1a As New MenuItem("Level 1.1.a", "mnuL1a", "")          mnuL1.MenuItems.Add(mnuL1a)          Dim mnuL1b As New MenuItem("Level 1.1.b", "mnuL1b", "")          mnuL1.MenuItems.Add(mnuL1b)      End Sub 

The preceding code creates the multilevel menu shown in Figure 9-13.

animal 9-13. Creating multilevel menus

Menu items that include a server-side event handler cause postback events when they are clicked. Items that don't include the handler (such as Figure 9-13) don't cause postbacks.

You can call client-side scripts from a menu item by specifying a client event handler as a string in the second argument of the MenuItem constructor. For example, the following code adds an Add Numbers menu item to the client Sum web part created in the previous section, "Working on the Client Side":

 Public Overrides Sub CreateWebPartMenu()          ' Create a menu item that runs a client-side script.          Dim mnuSum = New MenuItem("Add Numbers", _  "return _btn_onclick();"  , "mnuSum")          Me.WebPartMenu.MenuItems.Add(mnuSum)       End Sub 



Essential SharePoint
Essential SharePoint 2007: A Practical Guide for Users, Administrators and Developers
ISBN: 0596514077
EAN: 2147483647
Year: 2005
Pages: 153
Authors: Jeff Webb

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net