User Control Personalization

Last post 07-20-2005 7:50 PM by JoeBerg. 1 replies.

Sort Posts:

  • User Control Personalization

    07-07-2005, 10:34 AM
    • All-Star
      27,998 point All-Star
    • bmains
    • Member since 10-22-2004, 12:20 PM
    • Posts 5,647
    Hello,

    I have a user control that is a search form.  Within this user control, I have a radiobuttonlist that will most likely select the same value every time.  Can I tie that into personalization through code, or somehow within the user control, maybe through a custom property?

    Thanks.
    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: User Control Personalization

    07-20-2005, 7:50 PM
    • Participant
      880 point Participant
    • JoeBerg
    • Member since 08-20-2004, 9:08 AM
    • Redmond
    • Posts 176

    You can implement the IPersonalizable interface and manage your custom properties in those functions. Here's a basic example:

    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    namespace CustomControls
    {
        public class SimpleIPersonalizableWebPart : WebPart, IPersonalizable
        {
            private TextBox txtBox;

            public SimpleIPersonalizableWebPart()
            {
                txtBox = new TextBox();
                this.Title = "Default SimpleIPersonalizableWebPart Title";
            }

            protected override void CreateChildControls()
            {
                txtBox.ID = "txtId";
                txtBox.Text = "Default textbox Text";
                Controls.Add(txtBox);
            }

            void IPersonalizable.Load(PersonalizationDictionary state)
            {
                if (Controls.Count == 0 || state == null)
                    return;

                PersonalizationEntry entry = state["TextBoxTextValue"];
                if (entry != null)
                {
                    object obj = entry.Value;
                    if (obj.GetType().Name == "String" && Controls[0].GetType().Name == "TextBox")
                    {
                        ((TextBox)Controls[0]).Text = (string)obj;
                    }
                }
            }

            void IPersonalizable.Save(PersonalizationDictionary state)
            {
                foreach (Control control in Controls)
                {
                    if (control.GetType().Name == "TextBox")
                    {                
                        PersonalizationEntry entry = new PersonalizationEntry(((TextBox)control).Text, PersonalizationScope.User);
                        state.Add("TextBoxTextValue", entry);
                    }
                }
            }

            bool IPersonalizable.IsDirty
            {
                get
                {
                    return true;
                }
            }
        }
    }

Page 1 of 1 (2 items)