DropDownList in GUIPlugin

Vote:
 

I am creating a UserSettings tab for additional information based on these instructions: https://world.episerver.com/forum/legacy-forums/Episerver-CMS-version-5/Thread-Container/2007/11/EPiServerProfile--user-data/

I am creating the tab as an ascx  UserControl, which - I have to admit - I haven't worked with for a long time and don't really remember allt he ins and outs of it. 

Everything works as expected, except that a DropDownList that I am displaying is reset before the selected value is saved (epi calls OnLoad when I click on Save in the UI). 

Here's some snippets that hopefully shows enough to figure it out:

In the ascx (snippet of dropdown):  

<div>
    <asp:Label runat="server" AssociatedControlID="marketList">Market</asp:Label>
    <asp:DropDownList runat="server" ID="marketList" AutoPostBack="false" CssClass="episize240" Width="300px" DataTextField="Value" DataValueField="Key" />
</div>

The backend code:

	[GuiPlugIn(DisplayName = "Additional User Settings", Description = "Additional User Settings", Area = PlugInArea.SidSettingsArea, Url = "~/modules/UserSettings.ascx")]
	public partial class UserSettings : System.Web.UI.UserControl, IUserSettings, ICustomPlugInLoader
	{
		private readonly IDictionary<string, string> markets;
		public UserSettings()
		{
			var CVLService = new CVLService();
			markets = CVLService.GetCVL("Market");
		}

		private IDictionary<string, string> createMarketsList()
		{
			if (marketList.DataSource == null)
			{
				marketList.DataSource = markets;
				marketList.DataBind();
			}
			return markets;
		}
		public bool SaveRequiresUIReload { get; set; }

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			createMarketsList();
		}

		public void LoadSettings(string userName, EPiServerProfile data)
		{
			if (!IsPostBack)
			{
				createMarketsList();
				firstName.Text = data.FirstName;
				lastName.Text = data.LastName;
				avatar.Text = data.GetPropertyValue("Avatar").ToString();
				var market = data.GetPropertyValue("Market").ToString();
				if (string.IsNullOrWhiteSpace(market) || !markets.ContainsKey(market))
					market = "global";
				marketList.SelectedValue = market;
			}
		}


		public void SaveSettings(string userName, EPiServerProfile data)
		{
			data.FirstName = firstName.Text;
			data.LastName = lastName.Text;
			data.SetPropertyValue("Avatar", avatar.Text);
			data.SetPropertyValue("Market", marketList.SelectedValue);
			data.Save();
		}

		public PlugInDescriptor[] List()
		{
			return new[] { PlugInDescriptor.Load(typeof(UserSettings)) };
		}
	}

The other values works as expected. Is there a solution to this problem?

Peter

#250776
Mar 17, 2021 15:01
Vote:
 

I'm an idiot, turns out all I needed to do was to add EnableViewState="true" to the UserControl 🤦‍♂️

#250779
Mar 17, 2021 18:46
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.