November Happy Hour will be moved to Thursday December 5th.

"Remember" value of custom property

Vote:
 

So, I've created my first ever custom property - a dynamically filled DropDownList. Works great until I go back and edit an already saved page because it doesn't remember what my choice was. I'm sure there is some function I need to call to do this, I just don't know which one it is. Care to help me out? How do I set the selected value to the saved value from the last edit?

#49715
Mar 29, 2011 15:43
Vote:
 

Depends on how your code looks, but something like this:

public class PropertyWeekNumberDropDownControl : PropertySelectControlBase
{
	protected override void SetupEditControls()
	{
		this.SetupDropDownList();
	}

	private void SetupDropDownList()
	{
		this.EditControl.DataSource = GetWeekNumbers;

		if (this.WeekNumberDropDown.Value != null)
		{
			this.EditControl.SelectedValue = this.WeekNumberDropDown.Value as string;
		}

		this.EditControl.DataBind();
	}

	public PropertyWeekNumberDropDown WeekNumberDropDown
	{
		get
		{
			return PropertyData as PropertyWeekNumberDropDown;
		}
	}
}

    

#49718
Mar 29, 2011 16:00
Vote:
 

This should save your value:

 public override void ApplyEditChanges()
            {
                if (!String.IsNullOrEmpty(yourControl.SelectedValue))
                {
                    SetValue(yourControl.SelectedItem.Value);
                }
            }

 And then to set it you need to go through your options and then check if its equal the current value:

 foreach (var val in yourListOfOptions)
 {
 ListItem li = new ListItem(val, yourListOfOptions[val]);
   li.Selected = currentPropertyValue == yourListOfOptions[val];
 
   yourControl.Items.Add(li);
 }

 

 Something like that should do it I think

 

 

#49719
Mar 29, 2011 16:01
Vote:
 

That did the trick. I was uncertain how it would assume the Get would belong to the page being edited but with a function name that revealing I see the connection. Thanks guys!

#49720
Mar 29, 2011 16:48
Vote:
 

If someone might read this one still I've got another question. Is it possible to reach a dynamic property in the code for the custom property? I need to get the value of one to fill the dropdown. I usually just use CurrentPage["MyProperty"] but that won't work in this case.

#49734
Mar 30, 2011 9:42
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.