Adding Drop-Down Menu Items to SPGridView Using SPMenuField Control

http://sharepointmalaya.blogspot.com/2009/07/adding-drop-down-menu-items-to.html

protected override void CreateChildControls()
{
    // Why UseDefaultStyles = false?
    // See http://sharepointmalaya.blogspot.com/2009/07/how-to-make-spgridview-to-have-same.html for details
    this.UseDefaultStyles = false;

    // ObjectDataSource
    ObjectDataSource dataSource = new ObjectDataSource();
    dataSource.ID = “dataSource”;
    dataSource.SelectMethod = “SelectData”;
    dataSource.TypeName = this.GetType().AssemblyQualifiedName;
    dataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(objectCreating);
    this.Controls.Add(dataSource);

    // SPGridView
    spGridView = new SPGridView();
    spGridView.ID = “spGridView”;
    spGridView.DataSourceID = dataSource.ID;
    spGridView.AutoGenerateColumns = false;
    spGridView.AllowPaging = true;
    spGridView.PageSize = 10;
    spGridView.AllowSorting = true;
    spGridView.EmptyDataText = string.Empty;
    this.Controls.Add(spGridView);

    // SPGridViewPager
    pager = new SPGridViewPager();
    pager.GridViewId = spGridView.ID;
    this.Controls.Add(pager);

    // SPMenuField – Name field
    SPMenuField nameCol1 = new SPMenuField();
    nameCol1.HeaderText = “Name”;
    nameCol1.TextFields = “Name”;
    nameCol1.NavigateUrlFields = “Name,Email,WebSite”;
    nameCol1.NavigateUrlFormat = “/_layouts/sharepointmalaya/DispDetails.aspx?WebPartID=” + this.ID + “&FilterName={0}&ReturnUrl=” + Page.Request.Url.ToString();
    nameCol1.TokenNameAndValueFields = “KEYNAME=Name,KEYEMAIL=Email,KEYWEBSITE=WebSite”;
    nameCol1.SortExpression = “Name”;
    nameCol1.MenuTemplateId = “menuTemplate”;

    // MenuTemplate
    MenuTemplate menuTemplate = new MenuTemplate();
    menuTemplate.ID = “menuTemplate”;
    this.Controls.Add(menuTemplate);
    spGridView.Columns.Add(nameCol1);

    // MenuTemplate – View Item
    MenuItemTemplate menuItemTemplate0 = new MenuItemTemplate(“View Item”, “~/_layouts/images/LIST.GIF”);
    menuItemTemplate0.ClientOnClickNavigateUrl = “/_layouts/sharepointmalaya/DispDetails.aspx?WebPartID=” + this.ID + “&FilterName={%KEYNAME%}&ReturnUrl=” + Page.Request.Url.ToString();
    menuTemplate.Controls.Add(menuItemTemplate0);

    // MenuTemplate – Seperator
    MenuSeparatorTemplate menuSepTemplate = new MenuSeparatorTemplate();
    menuTemplate.Controls.Add(menuSepTemplate);

    // MenuTemplate – Open WebSite
    MenuItemTemplate menuItemTemplate1 = new MenuItemTemplate(“Open WebSite”, “~/_layouts/images/ASP16.GIF”);
    menuItemTemplate1.ClientOnClickNavigateUrl = “javascript:window.open(‘%KEYWEBSITE%’);”;
    menuTemplate.Controls.Add(menuItemTemplate1);

    // MenuTemplate – Send Email
    MenuItemTemplate menuItemTemplate2 = new MenuItemTemplate(“Send Email”, “~/_layouts/images/EML16.GIF”);
    menuItemTemplate2.ClientOnClickScript = “javascript:document.location.href=’MailTo:%KEYEMAIL%;'”;
    menuTemplate.Controls.Add(menuItemTemplate2);

    // BoundField – Email field
    BoundField nameCol2 = new BoundField();
    nameCol2.DataField = “Email”;
    nameCol2.SortExpression = “Email”;
    nameCol2.HeaderText = “Email”;
    spGridView.Columns.Add(nameCol2);

    // BoundField – WebSite field
    BoundField nameCol3 = new BoundField();
    nameCol3.DataField = “WebSite”;
    nameCol3.SortExpression = “WebSite”;
    nameCol3.HeaderText = “WebSite”;
    spGridView.Columns.Add(nameCol3);

    this.ChildControlsCreated = true;
}

  • Code above create three (3) drop-down menu items and a separator menu as shown below:

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s