Monday, 4 June 2012

SPMenuField disappears when/after Edit Mode

There has been an issue that has been bugging me when I create an SPMenuField, editing the page will result in the menu field columns disappearing.

After spending hours trialing and erroring, it was resolved by adding a .DataBind() in the Page_Load method.

UserControl.ascx.cs

        protected void Page_Load(object sender, EventArgs e)
        { 
          gvPPL.DataBind();
        }

UserControl.ascx


<SharePoint:MenuTemplate ID="gvPPLMenu" runat="server">
    <SharePoint:MenuItemTemplate ID="editMenu" runat="server" Text="Edit" ClientOnClickNavigateUrl="/_layouts/BPOSproperties/ProjectSettings.aspx?type=edit&url=%EDIT%" />
</SharePoint:MenuTemplate>
<SharePoint:SPGridView ID="gvPPL" runat="server" AutoGenerateColumns="false" DataSourceID="pplDataSource">
    <Columns>
        <SharePoint:SPMenuField HeaderText="NameMenu" TextFields="Name" MenuTemplateId="gvPPLMenu"
        NavigateUrlFields="Email" NavigateUrlFormat="{0}" TokenNameAndValueFields="EDIT=Email" />
        <SharePoint:SPBoundField HeaderText="Name" DataField="Name"  />
    </Columns>
</SharePoint:SPGridView>

<asp:ObjectDataSource ID="pplDataSource" runat="server" TypeName="BPOSsite.PeopleManager, BPOSsite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9ab07f514f0925b" SelectMethod="GetPeople"  />

People.cs


namespace BPOSsite
{
    public class People
    {
        private string name;
        private string email;

        public People(string n, string e)
        {
            this.email = e;
            this.name = n;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
    }

    public class PeopleManager
    {
        public List<People> GetPeople()
        {
            List<People> list = new List<People>();
            list.Add(new People("John", "john@mail.com"));
            list.Add(new People("Nick", "nick@mail.com"));
            list.Add(new People("Tracy", "tracy@mail.com"));
            list.Add(new People("Dick", "dick@mail.com"));
            return list; 
        }    
    }
}

No comments:

Post a Comment