+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Getting existing property value from WebUITypeEditor

Getting existing property value from WebUITypeEditor

5 posts, 0 answered
  1. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    16 Aug 2010
    Link to this post
    Hi all,

    I've been developing a WebUITypeEditor which will allow the user to select a number of documents from any of their document libraries (see attachment grid.jpg). The WebUITypeEditor returns the guid's of all the documents that were selected. The documents that the user has selected are stored in a RadGrid. To get the value of all of the selected documents the following code is executed:

    01.public override Guid[] Value {
    02. 
    03.        get {
    04. 
    05.            //setup local variables for use
    06.            Guid guDocID = Guid.Empty;
    07.            List<Guid> guDocIDs = new List<Guid>();
    08. 
    09.            //loop through each row in grid
    10.            foreach (GridDataItem item in GridSelectedDocuments.MasterTableView.Items) {
    11. 
    12.                //add to local variable
    13.                guDocID = new Guid(item["DocumentID"].Text);
    14.                guDocIDs.Add(guDocID);
    15. 
    16.            }
    17. 
    18.            //return array of guids
    19.            return guDocIDs.ToArray();
    20.        }
    21. 
    22.        set {
    23.            this.ViewState["DocumentIDs"] = value;
    24.            base.ChildControlsCreated = false;
    25.        }
    26. 
    27.    }

    This works fine. As you can see from the attachment selector.jpg, the guid's are returned correctly.

    The problem I'm having is that when the WebUITypeEditor is activated at a later date/time, I want the user to be able to edit the list of documents they previously selected. For this to happen, I need to be able to get the value of "Value" when the control loads. I've created a simple bullet list to better explain this scenario.

    1) User opens WebUITypeEditor and selects three documents.
    2) User clicks "Im done" - documents are shown on web page.
    3) At a later time, the user clicks Edit so they can add a fourth document to the existing list.

    At point 3 I dont know how to get hold on the guid's that are already stored. Do i need to do something like parent.Findcontrol("DocumentIDs") to get hold of the values shown in selector.jpg?

    Thanks in advance
    higgsy

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Aug 2010
    Link to this post
    Hi Alastair,

    Options

    1.Get  the value for the property and re select all check boxes in the RadGrid when you open the selector, then when you click 'I'm done" of the WebUI type editor the values will be set again to Value property.

    2. You can save the values in a HiddenFiled and then re add them to the entire list so you will not loose any guid.

    3. Create another property that is not Browsable and keep a record for the items there.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Paritosh Arya
    Paritosh Arya avatar
    10 posts
    Registered:
    18 Dec 2009
    24 Aug 2010
    Link to this post
    I have been hitting my head against the wall but still haven't figured this out. I can set the value of my control, but when I try to edit and get the existing property value from WebUITypeEditor, it's always empty.
     To put it simply, the viewstate get's loaded after I have gone through the code to set the textbox property. Here's my code... I hope you guys can help me out.

    1) Custom bulleted list control code for complex property -
    private List<string> _listItems;
            [Category("List settings")]
            [WebEditor("SubServicingControls.SaxonBulletedListEditor, SubServicingControls"), Browsable(true)]
            [TypeConverter("SubServicingControls.ListConverter, SubServicingControls")]
            public List<string> ListItems
            {
                get
                {
                    if (_listItems == null)
                        _listItems = new List<string>();
                    return _listItems;
                }
                set
                {
                    _listItems = value;
                }
            }

    2) Code in the editor class (excluding the namespace) -
    public class SaxonBulletedListEditor : WebUITypeEditor<List<string>>
        {
            private TextBox txtData;
            private Button btnDone;
            private Panel editorPanel;
            string textBoxData;
              
            public SaxonBulletedListEditor()
            {
                this.EnsureChildControls();
                this.EnableViewState = true;
            }
      
            protected override void  CreateChildControls()
            {
                 base.CreateChildControls();
                Controls.Clear();
                editorPanel = new Panel();
      
                txtData = new TextBox();
                txtData.ID = "txtItemData";
                txtData.TextMode = TextBoxMode.MultiLine;
                txtData.Rows = 10;
                editorPanel.Controls.Add(txtData);            
      
                btnDone = new Button();
                editorPanel.Controls.Add(txtData);
                btnDone.Text = "Save List Data";
                btnDone.ID = "btnFinish";
                btnDone.Click += new EventHandler(btnDone_Click);
                editorPanel.Controls.Add(btnDone);
      
                Controls.Add(editorPanel);
            }
      
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                txtData.Text = GetListData(Value);
            }
      
            private List<string> items
            {
                get
                {
                    if (ViewState["items"] == null)
                    {
                        items = new List<string>();
                        return items;
                    }
                    else
                    {
                        return ViewState["items"] as List<string>;
                    }
      
                }
                set
                {
                    ViewState["items"] = value;
                }
            }
      
            public override List<string> Value
            {
                get
                {
                    return items;
                }
                set
                {
                    items = value;
                }
            }
      
            void btnDone_Click(object sender, EventArgs e)
            {
                textBoxData = txtData.Text;
                items = BuildListFromTexbox(textBoxData);
            }
      
            private List<string> BuildListFromTexbox(string data)
            {
                List<string> stringItems = new List<string>();
      
                string[] listData = data.Split(';');
      
                foreach (string li in listData)
                {
                    if (!string.IsNullOrEmpty(li))
                        stringItems.Add(li);
                }
      
                return stringItems;
            }
      
            private string GetListData(List<string> l)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string s in l)
                    sb.Append(s).Append(";");
      
                return sb.ToString();
            }
        }


    3) In case if needed, the converter code is -
    /// <summary>
        /// This class implements methods that convert string representation of List
        /// to List object and vice versa - converter will convert only list
        /// where list items are of type string
        /// </summary>
        public class ListConverter : TypeConverter
        {
            /// <summary>
            /// Returns whether this converter can convert an object of the given type to
            /// the type of this converter, using the specified context.
            /// </summary>
            /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
            /// <param name="sourceType">A System.Type that represents the type you want to convert from.</param>
            /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
            }
            /// <summary>
            /// Returns whether this converter can convert the object to the specified type,
            /// using the specified context.
            /// </summary>
            /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
            /// <param name="destinationType">A System.Type that represents the type you want to convert to.</param>
            /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
            }
            /// <summary>
            /// Converts the given object to the type of this converter, using the specified
            /// context and culture information.
            /// </summary>
            /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
            /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param>
            /// <param name="value">The System.Object to convert.</param>
            /// <returns>An System.Object that represents the converted value.</returns>
            /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception>
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                // the logic in this method will convert string into a Dictionary. The string has following
                // format:
                // key1=value1;key2=value2
                // where semi-color (;) separates dictionary items, while equal sign (=) separates
                // item key from item value
                if (value is string)
                {
                    List<string> list = new List<string>();
      
                    // split string by semi-colon (;) to get array of list items
                    string[] listItems = ((string)value).Split(';');
                    foreach (string listItem in listItems)
                    {
                        // add new item to the list
                        if(!string.IsNullOrEmpty(listItem))
                            list.Add(listItem);
                    }
                    return list;
                }
                return base.ConvertFrom(context, culture, value);
            }
            /// <summary>
            /// Converts the given value object to the specified type, using the specified
            ///     context and culture information.
            /// </summary>
            /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
            /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param>
            /// <param name="value">The System.Object to convert.</param>
            /// <param name="destinationType">The System.Type to convert the value parameter to.</param>
            /// <returns>An System.Object that represents the converted value.</returns>
            /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception>
            /// <exception cref="System.ArgumentNullException">The destinationType parameter is null.</exception>
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                // the logic in this method will convert List into a string (serialize the List).
                //The string has following format:
                // item1;item2
                // where semi-color (;) separates list items
                if (destinationType == null)
                    throw new ArgumentNullException("destinationType");
                if ((destinationType == typeof(string)) && (value is List<string>))
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (string listItem in (List<string>)value)
                    {
                        sb.AppendFormat("{0};", listItem);
                    }
                    return sb.ToString();
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }


    I am still struggling in populating the textbox (txtData) with the property value. It always shows up empty.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    25 Aug 2010
    Link to this post
    Hello Paritosh Arya,

    1. The Value property should be a ViewState property
    2. You should call the Value property inside CreateChildControls and populate the selection with the ViewState data.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Paritosh Arya
    Paritosh Arya avatar
    10 posts
    Registered:
    18 Dec 2009
    25 Aug 2010
    Link to this post
    Thanks for your reply but that still didn't work. Then I went with a different solution, while setting the Value in viewstate as you suggested, of finding control at PreRender event and then setting is and that worked :-)
Register for webinar
5 posts, 0 answered