Sometime we need to customize the menu of a list view web part to implement our own feature.
For example, dynamically change the script of new menu, update menu, etc.
What we shall do is to get the menu object and change its attribute "ClientOnClickScript" or "ClientOnClickNavigateUrl".
From Reflector, we can see the source code as follows:
[DefaultValue(""), Category("Behavior")] public string ClientOnClickScript { get { return this.m_clientOnClickScript; } set { if (value == null) { value = string.Empty; } this.m_clientOnClickScript = value; this.ViewState["ClientOnClickScript"] = value; this.m_clientOnClickNavigateUrl = null; this.ViewState["ClientOnClickNavigateUrl"] = null; this.clientOnClickUsingPostBackEvent = null; } }
[DefaultValue(""), Category("Behavior")] public string ClientOnClickNavigateUrl { get { return this.m_clientOnClickNavigateUrl; } set { if (value == null) { value = string.Empty; } value = SPUtility.GetServerRelativeUrlFromPrefixedUrl(value); value = value.Replace("'", @"/'"); this.ClientOnClickScript = "window.location = '" + value + "';"; this.m_clientOnClickNavigateUrl = value; this.ViewState["ClientOnClickNavigateUrl"] = value; } }
In the set method, it will change the other one. That is to say, we can select either to get our feautre done.
Primary code is below:
foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in this.WebPartManager.WebParts) { if (webpart is ListViewWebPart) { ListViewWebPart listViewWebPart = (ListViewWebPart)webpart; ViewToolBar myViewToolBar = null; foreach (Control control in listViewWebPart.Controls) { if (control is ViewToolBar) { myViewToolBar = (ViewToolBar)control; } } ToolBar myToolBar = (ToolBar)myViewToolBar.Controls[0].FindControl("toolBarTbl"); IEnumerator menus = myToolBar.Buttons.Controls.GetEnumerator(); while (menus.MoveNext()) { if (menus.Current is ActionsMenu) { } else if (menus.Current is NewMenu) { NewMenu newMenu = (menus.Current as NewMenu); MenuItemTemplate newFileMenu = newMenu.MenuTemplateControl.FindControl("New0") as MenuItemTemplate; if (newFileMenu != null) { newFileMenu.Visible = false; } MenuItemTemplate newFolderMenu = newMenu.MenuTemplateControl.FindControl("NewFolder") as MenuItemTemplate; if (newFolderMenu != null) { //newFolderMenu.ClientOnClickNavigateUrl = "/_layouts/LibraryTree/CreateFolder.aspx?mURL=" + redirectURL + "&FolderURL=" + folderPath + "&ListID=" + mListID; newFolderMenu.ClientOnClickScript = "RedirectToURL('" + redirectURL + "','" + folderPath + "','" + mListID + "')"; } } else if (menus.Current is UploadMenu) { UploadMenu uploadMenu = (menus.Current as UploadMenu); MenuItemTemplate singleUploadMenu = uploadMenu.MenuTemplateControl.FindControl("Upload") as MenuItemTemplate; if (singleUploadMenu != null) { singleUploadMenu.ClientOnClickNavigateUrl = "/_layouts/LibraryTree/UploadFile.aspx?mURL=" + redirectURL + "&FolderURL=" + folderPath + "&ListID=" + mListID; //singleUploadMenu.ClientOnClickScript = "window.location.href='/_layouts/LibraryTree/UploadFile.aspx?mURL=" + redirectURL + "&FolderURL=" + folderPath + "&ListID=" + mListID + "';"; } MenuItemTemplate multiUploadMenu = uploadMenu.MenuTemplateControl.FindControl("MultipleUpload") as MenuItemTemplate; if (multiUploadMenu != null) { multiUploadMenu.Visible = false; } } } } }
The Microsoft doesn't suggest this way, but it is dynamic.
Hava a good time.